-
Notifications
You must be signed in to change notification settings - Fork 0
/
STL
70 lines (40 loc) · 3.07 KB
/
STL
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//Part of #include <algorithm>
Important STL Functions to save Time
1->max({a, b, c, d}); ->Max of multiple elements
2->max_element(start, end) ->max in range
3->count(begin, end) ->just as name
4->accumulate(begin, end) ->sum of elements in range
5->merge(range1 begin , range1 end, range2 begin, range2 end) ->merge two sorted ranges.
6->unique(.begin(), .end()) ->
std::unique(begin, end) goes from begin to end, removes consecutive duplicates and returns the end of the resulting iterator range. So, for example, if vec = {1, 1, 2, 2, 3, 2, 1}, then unique(all(vec)) makes it equal {1, 2, 3, 2, 1, ?, ?} (where ? is not known, but probably the last two symbols are 2 and 1) and returns vec.begin() + 5. Usually, to make a vector containing only its unique elements
7->fill(first,last,val)->fill range [first,last] with val.
8->iota(begin,end,initial_value)->Assigns to every element in the range [begin, end] successive values of initial_value, as if incremented with ++initial_value after each element is written.
******************************************************************MORE*******************************************************************
->binary_search(range begin,range end, key)->return true if key found in given range else false;
->lower_bound(range begin,range end,key)->returns iterator to first element not smaller than key.
(if key found returns iterator to key else returns iterator to next greater element)
(returns iterator to end of range if neither key nor greater element than key are found.)
->upper_bound(range begin, range end, key)-> returns iterator to first element greater than key.
(never returns iterator to key even if found. always returns iterator to next greater element.)
(returns iterator to end of range if greater element than key not found.)
{
//Example Implementation of lower_bound && upper_bound
A---> a[8]={1,2,3,3,4,6,7,8}; //Given ARRAY
B-> lower_bound(a,a+8,3);
C-> upper_bound(a,a+8,3);
Statement B will return iterator to 2nd index (first occurence of 3);
2nd Case: Suppose if 3 didn't existed in array then lowerbound would have returned an iterator to 4(next greater element than key).
3rd Case: If neither key nor any element greater than key exist then returns iterator to the end of array
Statement C will return iterator to 4th index(first element greater than 3);
2nd Case: If neither key nor any element greater than key exist then returns iterator to the end of array
}
Containers->
map
unordered_map
multimap
unordered_multimap
set
multiset
unordered_set
unordered_multiset
Read more (in depth) at cpluscplus.com