0% found this document useful (0 votes)
3 views

STL 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

STL 1

Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++ STL (Part 1)

- Srivaths P
Goal
• To learn about containers
• pair
• vector
• set, unordered_set
• map, unordered_map
About STL
Standard Template Library (STL) is a set of C++
functions/classes to perform various tasks.

There is a wide variety of functions and classes


for different applications.

STL objects are more efficient, bug-free, and


easier to use than custom implementations.
Pair
Pairs are very useful when dealing with two related
values. For example, storing a range [L, R].

Pairs have inbuilt comparators such as <, >, etc.

Usage: pair<int, int> p = {1, 7};


cout << p.first << endl; // outputs 1
cout << p.second << endl; // outputs 7
Vector
Vectors store an ordered collection of data.
Unlike arrays, vectors can be resized. They also
have far more features than arrays.

Useful vector functions:


v.begin(), v.end();
v.push_back(val), v.pop_back();
v.empty(), v.size();
v.insert(it, val), v.erase(it);
v.clear();
Sort Function
Syntax: sort(begin, end, comparator);
Sorts elements from [begin_iterator, end_iterator)

It is possible to write our own sort criteria instead


of ascending/descending. It is called a
“comparator”.
Set / Map
Sets store unique values in a sorted order.
Search, removal, insertion of an element is O(logN).

For sets to work for some datatype, the datatype must


have “<” function implemented.

Maps store a value for a unique key (sorted by the key).


Search, removal, insertion of an element is O(logN).

In other words, maps are similar to vectors, but they can


have any value as an index. Also, they are sorted by index.
Unordered Set
Unordered Sets store unique values, in any order.
Search, removal, insertion of an element is O(1).

For unordered sets to work for some datatype, the


datatype must have a hash function implemented.

Therefore, unordered set can’t store pairs, or vectors, or


other datatypes without a hash function.
Unordered Map
Unordered Map is similar to maps, but the keys are not
ordered. Search, removal, insertion of an element is O(1).

For unordered maps to work for some datatype for the


key, the datatype must have a hash function
implemented.

Similar to unordered set, unordered map does not work


for pair, vector, etc.
Example Problems:
• https://codeforces.com/group/c3FDl9EUi9/conte
st/262795/problem/B

• https://codeforces.com/group/c3FDl9EUi9/conte
st/262795/problem/C

• https://codeforces.com/group/c3FDl9EUi9/conte
st/262795/problem/D
Resources:
https://www.cppreference.com/Cpp_STL_ReferenceManual.pdf
https://devdocs.io/cpp/container (for STL containers)
https://devdocs.io/cpp/algorithm (for STL algorithms)

Using the above resources, try to learn about


multiset, multimap.

You might also like