C++ STL - Cheat Sheet
C++ STL - Cheat Sheet
Introduction
The Standard Template Library (STL) is a C++ library that contains all class and function templates. It
is used for implementing the basic Data Structures (like Hashset, Heap, List, etc.) and functions (like
math, algorithms, etc.), and contains all containers available in C++ programming language.
Components of STL
Standard Template Library contains of several containers and functions, and can be categorized in the
following manner −
Containers
Functors
Algorithms
Iterators
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified
expert to boost your career.
STL Containers
The STL container template class contains data structures like Dynamic Arrays (or Vectors, Lists),
Hashmaps, Hashsets, Trees, Linked Lists, etc. These are used for storing and performing operations
on the data.
Sequential Containers
Container Adapters
Associative Containers
Unordered Containers
Each container has its respective header file, which can be included at the start of the program. For
example, the std::vector is included in the #include<vector> library.
Page 2 of 28
Sequential Containers
The sequential containers implement the data structures with sequential access. These include −
Vector
List
Deque
Array
Forward List
Container Adapters
The container adapters implement data structures like queues, stacks, etc by providing different
interfaces for sequential containers. These include −
Stack
Queue
Priority Queue
Associative Containers
The associative containers are used to store ordered data that can be quickly searched using their key
value. These include −
Set
Multiset
Map
Multimap
Unordered Containers
The unordered containers are similar to associative containers but the only difference is that they
don’t store sorted data. Still, these containers provide quick search time using key-value pairs. They
are −
Unordered Set
Unordered Multiset
Page 3 of 28
Unordered Map
Unordered Multimap
Now that we have established a learning graph of all containers, we will briefly explain each container
in the STL with an exemplar code −
Vector in STL
The vector is initialized as a dynamic array at runtime, and its size is variable. It can be found in the
C++ <vector> header file.
Syntax
There are different functions in the vector template. These are explained briefly in the table below −
2. end() Returns an iterator to the theoretical element after the last element. O(1)
Here, TC indicates time complexity of different member functions of the vector template. For more
information on time complexity, visit this article − Time complexity.
Example
Open Compiler
int main(){
int n=2;
vector<int> vec1 = { 1, 2, 3, 4, 5 };
vector<int> vec2(n,0);
vector<vector<int>> vec3(n,vector<int>(2*n,1));
vec1.push_back(6);
vec1.erase(vec1.begin() + 4);
vector<pair<int,int>> vec4;
vec4.push_back({2,3});
vec4.push_back({4,3});
Output
vec1: 1 2 3 4 5 6
vec2: 0 0
vec1 after erasing: 1 2 3 4 6
vec3:-
1111
1111
List in STL
The list container is initialized as a doubly linked list, whereas for implementing a singly linked list,
we use a forward_list. It can be found in the C++ <list> header file.
Syntax
list<data_type> list1;
There are different functions in the list template. These are explained briefly in the table below −
Page 6 of 28
2. end() Returns an iterator to the theoretical element after the last element. O(1)
10. remove() Removes all the copies of the given elements from the list. O(n)
Example
Open Compiler
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main(){
list<int> list1 = { 1, 5, 9, 1, 4, 6 };
cout << "List1 first and last element: " << list1.front() <<" "
<<list1.back()<<endl;
// adding element
list1.insert(list1.begin(), 5);
// deleting element
list1.erase(list1.begin());
// traversing list1
Page 7 of 28
return 0;
}
Output
Deque in STL
The deque container is initialized as a doubly ended queue, where elements can be pushed and
popped from both ends of the queue. It can be found in the C++ <deque> header file.
Syntax
deque<data_type> dq1;
There are different functions in the deque template. These are explained briefly in the table below −
2. end() Returns an iterator to the theoretical element after the last element. O(1)
Example
Open Compiler
#include <deque>
#include <iostream>
using namespace std;
int main(){
deque<int> dq = { 1, 2, 3, 4, 5 ,6, 8 };
cout<<"Initial Deque: "<<endl;
for (auto i : dq) {
cout << i << " ";
}
cout<<endl;
dq.push_front(dq.back());
dq.pop_back();
for (auto i : dq) {
cout << i << " ";
}
cout<<endl;
dq.push_front(dq.back());
dq.pop_back();
for (auto i : dq) {
cout << i << " ";
}
cout<<endl;
dq.pop_back();
dq.pop_front();
for (auto i : dq) {
Page 9 of 28
dq.push_front(11);
dq.push_back(99);
return 0;
}
Output
Initial Deque:
1234568
8123456
6812345
81234
11 8 1 2 3 4 99
Stack in STL
The stack container is initialized as a LIFO container, where elements can be pushed to the top, and
popped from the top. Hence, the last element to enter is the first one to exit from the container. It can
be found in the C++ <stack> header file.
Syntax
stack<data_type> s1;
There are different functions in the stack template. These are explained briefly in the table below −
Example
Open Compiler
int main(){
stack<int> s;
s.push(2);
s.push(9);
s.push(3);
s.push(1);
s.push(6);
while (!s.empty()) {
cout<<"size is: "<<s.size()<<" ";
cout << "element is: "<<s.top() << endl;
s.pop();
}
return 0;
}
Output
Top is: 6
size is: 5 element is: 6
size is: 4 element is: 1
size is: 3 element is: 3
Page 11 of 28
Queue in STL
The queue container is initialized as a FIFO container, where elements can be pushed to the front, and
popped from the front. Hence, the first element to enter is the first one to exit from the container. It
can be found in the C++ <queue> header file.
Syntax
queue<data_type> q1;
There are different functions in the queue template. These are explained briefly in the table below −
Example
Open Compiler
#include <iostream>
#include <queue>
using namespace std;
int main(){
queue<int> q;
q.push(1);
q.push(1);
q.push(6);
Page 12 of 28
q.push(1);
return 0;
}
Output
Front element: 1
Back element: 1
q: 1 1 6 1
Hash-Set/Set in STL
The set container is initialized as unique element storage data structure, which also implements a
sorted order, both ascending and descending. It generally implements a red-black tree as an
underlying data structure. It can be found in the C++ <set> header file.
Syntax
set<data_type> set;
set<data_type, greater<data_type>> set2; //this is a set in descending
order
set<data_type, comparator/lambda_function> set3; //this is a set in
custom order
There are different functions in the set template. These are explained briefly in the table below −
Page 13 of 28
Example
Open Compiler
#include <iostream>
#include <set>
#include <vector>
using namespace std;
int main(){
set<int> set;
set.insert(9);
set.insert(11);
set.insert(9);
set.insert(11);
bool flag=set.find(9)!=set.end();
set.insert(21);
}
cout << endl;
return 0;
}
Output
Hash-Map/Map in STL
The map is a container which stores data in form of key-value pairs in a sorted order of keys, where
each key is unique. It is implemented using the red-black tree data structure. It is included in the
<map> header file.
Syntax
map<key_type,value_type> map;
There are different functions in the map template. These are explained briefly in the table below −
5. erase(iterator) Removes the element at the position pointed by the iterator. O(logn)
6. erase(key) Removes the key and its value from the map. O(logn)
Example
Open Compiler
Page 15 of 28
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(){
map<char,int> map;
string s="abbacdbbac";
for(char c: s){
map[c]++;
}
for(auto it:map){
cout<<it.first<<" : "<<it.second<<endl;
}
cout<<"after erasing element 'c' : "<<endl;
map.erase('c');
for(auto it:map){
cout<<it.first<<" : "<<it.second<<endl;
}
return 0;
}
Output
a:3
b:4
c:2
d:1
after erasing element 'c' :
a:3
b:4
d:1
Syntax
Page 16 of 28
unordered_set<data_type> set;
There are different functions in the unordered_set template. These are explained briefly in the table
below −
S. Time
Functions Description
No. Complexity
Example
Open Compiler
#include <iostream>
#include <unordered_set>
#include <vector>
using namespace std;
int main(){
unordered_set<int> set;
set.insert(19);
set.insert(10);
set.insert(13);
set.insert(21);
bool flag=set.find(9)!=set.end();
set.insert(21);
Output
Syntax
There are different functions in the unordered_map template. These are explained briefly in the table
below −
S. Time
Function Description
No. Complexity
S. Time
Function Description
No. Complexity
6. bucket() Returns the bucket number where the data is stored. O(1)
Example
Open Compiler
#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;
int main(){
unordered_map<char,int> map;
string s="abbacdbbac";
for(char c: s){
map[c]++;
}
for(auto it:map){
cout<<it.first<<" : "<<it.second<<endl;
}
cout<<"after erasing element 'c' : "<<endl;
map.erase('c');
for(auto it:map){
cout<<it.first<<" : "<<it.second<<endl;
}
return 0;
}
Output
d:1
c:2
b:4
Page 19 of 28
a:3
after erasing element 'c' :
d:1
b:4
a:3
equal_to
not_equal_to
greater
less
plus
minus
Member functions
Non-member functions
Operator classes
Example
Open Compiler
Page 21 of 28
#include <functional>
#include <iostream>
using namespace std;
int main(){
equal_to<int> obj1;
not_equal_to<int> obj2;
greater<int> obj3;
less<int> obj4;
plus<int> obj5;
minus<int> obj6;
return 0;
}
Output
The <algorithm> library in C++ offers many useful functions. Some of the most commonly used
functions are given below −
Sort
Copy
Find
For Each
Swap
sort() in C++
sort() algorithm is used to sort the given data in ascending, descending or custom order (using
comparator or lambda function).
Syntax
sort(start_iterator,end_iterator)
sort(start,end, comparator_function) //for custom sorting
Example
Open Compiler
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main(){
vector<int> v = {999, 1252, 3117, 122222 , 10, 88, 2, 9, 45, 82,
546, 42, 221 , -1};
cout<<"Ascending order: ";
sort(v.begin(),v.end());
for(int i:v) cout<<i<<" ";
cout<<endl;
Page 23 of 28
Output
copy() in C++
copy() algorithm is used to copy the elements from one container to another.
Syntax
copy(start_iterator,end_iterator, destination_operator)
Example
Open Compiler
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main(){
vector<int> v = { 1, 2, 3, 4, 5 , 4 ,3 , 2, 1};
vector<int> newvec(9);
copy(v.begin(), v.end(), newvec.begin());
return 0;
}
Output
123454321
find() in C++
find() algorithm is used to find a key element in a given range of elements.
Syntax
Example
Open Compiler
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main(){
vector<int> v= { 1,3,5,2,3,1,55,41};
// finding 5
auto itr = find(v.begin(), v.end(), 55);
if (itr != v.end()) {
cout << *itr << " Element found !!!" << endl;
}else {
cout << "Element not present !!!" << endl;
}
return 0;
}
Page 25 of 28
Output
Syntax
Example
Open Compiler
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main(){
vector<int> v = {999, 1252, 3117, 122222 , 10, 88, 2, 9, 45, 82,
546, 42, 221 , -1};
Output
for_each in C++
Syntax
Example
Open Compiler
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
using namespace std;
int main(){
vector<int> vec1 = { 1, 2, 3, 4, 5 };
return 0;
}
Output
23456
swap() in C++
Page 27 of 28
swap() algorithm is used to replace one element with another in place, hence the elements swap
places.
Syntax
swap(container1,container2)
Example
Open Compiler
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
swap(vec1,vec2);
cout<<"vec 1: "<<endl;
for(int i:vec1) cout<<i<<" ";
cout<<endl;
cout<<"vec 2: "<<endl;
for(int i:vec2) cout<<i<<" ";
cout<<endl;
return 0;
}
Output
vec 1:
456
vec 2: 1 2 3
Iterators can be considered as pointers that are used to iterate over containers sequentially. These
iterators are included using the <iterator> header file in C++.
Each container has its own iterator. For avoiding confusion, we can use ‘auto’ keyword to define an
iterator while traversing a container.
Input Iterator
Output Iterator
Forward Iterator
Bi-Directional Iterator
Random Iterator
Example
Open Compiler
#include <bits/stdc++.h>
using namespace std;
int main(){
vector<int> myvec={1,5,2,4,3,6,6,9,8};
set<int>set(myvec.begin(),myvec.end());
return 0;
}
Output
152436698
12345689