Professional Documents
Culture Documents
Containers
Sequential Containers
pair
vector
Ordered Containers
map
multi_map
set
multi_set
Unordered Containers
unordered_set
unordered_map
Iterators
Algorithms
Functors
Containers:
Pairs:
----------------------------------------------------------------------
vector <int> v(5);
v.push_back(7);
// Here, 7 is the 6th element. v -> 0 0 0 0 0 7
----------------------------------------------------------------------
// Passed by value
void print (vector <int> v) {
}
// Passed by reference
void print (vector <int> &v) {
}
In the latter case, all changes made to v in the function reflect in
the actual vector too, since it was passed by reference.
Reference:
Nesting In Vectors: C++ STL For Beginners | Competitive Programming…
The below code also performs the same activity as above
Reference:
Nesting In Vectors: C++ STL For Beginners | Competitive Programming…
Vectors have indexing of elements (i.e., each element is given some
index). So, in vectors, we can access elements using random access.
But, some other Data Structures like Maps and Sets don’t have
indexing. Their elements can only be accessed using iterators and we
can traverse through the DS using iter++;
Maps:
Maps store <key, value> pairs. i.e., map is a collection of pairs.
They create a mapping from keys to pairs.
Maps:
Not only does insertion take log n time, even accessing takes log n
time in Maps.
Use set and map only when you want to store in Ascending order of
elements. Otherwise, use unordered_set and unordered_map. Be aware
that unordered_set and unordered_map are quite dangerous to use, their
T.C. might not be O(1) if the test cases are designed in such a way
that after Hashing, same result comes for many elements.
Avoid using unordered_map and unordered_set as much as possible
(Especially in CP where people try to hack your solution)
maps and sets use Red and Black Trees (Balanced Trees) to store,
therefore, almost every operation takes O(log n) worst-case time.
unordered_map can be used only with the key being either a primitive
type or string, others can’t be used as key of unordered_map as their
Hash Value can’t be computed. In such cases we have to use maps,
because in maps we just compare elements and put it in a Tree DS.
Maps are generally used when we have to find the frequency of elements
Sets:
Sets are a collection of unique values. Main difference b/w maps and
sets is: maps are collections of pairs. sets are collections of unique
values, not pairs.
Again, sets can have any data type, i.e., set <pair<int, vector<int>>>
is possible like maps. And at the same time, like unordered_maps, they
can’t store complex data types, and can only store primitive + string
data types. Only unordered_set <int>,...unorderd_set <string> are
correct.
Sets are usually used when we want to find the unique elements in a
given input.
Maps are usually used when we want to find the frequency of each
element of the input.
Use unordered_set when you only want to store unique elements and the
order of storing doesn’t matter.
Use set when you want to store elements in an increasing order.
Multi-Set:
Allow storing the same value multiple times. It has the internal
implementation of set (i.e., using Red and Black Trees {Self-Balanced
Trees}). Since again Red and Black Trees are being used, the T.C. of
most operations is O(log n).
When we use .find (ele) method with multiset, the iterator of the
first occurrence of the element ele is returned.
Stack:
We can create a stack of any data type (int, float, string, box,..)
Queues:
Iterators:
// iterator object to a vector declaration:
vector <int> :: iterator it;
// It’s initialization:
it = v.begin();
// To know value at iterator:
cout << *it;
Another alternative:
The above code snippet shows how to use for-each / range based loops
for vector of pairs.
In the below case ‘value’ is a variable of data type = pair <int, int>
Algorithms:
Sorting:
Inbuilt sorting algorithm exist. Using which we can sort arrays,
vectors,...
We can also create our own sorting algorithm using comparator function
(That is the criteria on basis of which we need the sorting can be
specified in the comparator function and sorting is done on that
basis)
Inbuilt sort:
int A[n];
sort(A, A+n);
// sort () uses intro sort - which is a mixture of quick sort, heap
sort and insertion sort.
bool cmp () {
// Body of comparator function
}
reverse() function:
CP Notes:
CC Websites have a limitation on the amount of continuous memory that
can be allocated.
Max. size of array declared locally = 10^5
Max. size of array declared globally = 10^7
These same limits apply to vectors too as these are not limitations of
the arrays, but they are limitations of continuous memory allocation
Important Results
Useful for CP:
lcm (a,b) = a*b/(gcd(a,b))
Use __gcd (a,b) and lcm (a,b) in C++ for inbuilt implementations.