C++ STL – Cheat Sheet
”; Previous Next
This C++ STL cheatsheet covers a wide range of topics from basic STL like vectors, hashmaps, sets, etc., to advanced concepts like functors, iterators, and so on. It is designed for programmers who want to quickly read through key concepts along with the syntax and related examples.
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
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.
The container template has the following components −
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.
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
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
vector<data_type> vec1; //1D vector
vector<vector<data_type>> vec2; //2D vector
vector<container_type<data_type>> vec3; //2D vector of other container
vector<data_type> vec1(n); //vector of size n
vector<data_type> vec1(n,k); // vector of size n, with each element=k
There are different functions in the vector template. These are explained briefly in the table below −
S.No.
Function
Function Explanation
TC
1.
begin()
Returns an iterator to the first element.
O(1)
2.
end()
Returns an iterator to the theoretical element after the last element.
O(1)
3.
size()
Returns the number of elements present.
O(1)
4.
empty()
Returns true if the vector is empty, false otherwise.
O(1)
5.
at()
Return the element at a particular position.
O(1)
6.
assign()
Assign a new value to the vector elements.
O(n)
7.
push_back()
Adds an element to the back of the vector.
O(1)
8.
pop_back()
Removes an element from the end.
O(1)
9.
insert()
Insert an element at the specified position.
O(n)
10.
erase()
Delete the elements at a specified position or range.
O(n)
11.
clear()
Removes all elements.
O(n)
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
// C++ program to illustrate the vector container
#include <iostream>
#include <vector>
using namespace std;
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);
cout << “vec1: “;
for (int i = 0; i < vec1.size(); i++) {
cout << vec1[i] << ” “;
}
cout << endl<<“vec2: “;
for (int i = 0; i < vec2.size(); i++) {
cout << vec2[i] << ” “;
}
cout << endl;
vec1.erase(vec1.begin() + 4);
cout << “vec1 after erasing: “;
for (auto i = vec1.begin(); i != vec1.end(); i++) {
cout << *i << ” “;
}
cout << endl;
cout << “vec3:-” << endl;
for (auto i : vec3) {
for (auto j : i) {
cout << j << ” “;
}
cout << endl;
}
cout << endl;
vector<pair<int,int>> vec4;
vec4.push_back({2,3});
vec4.push_back({4,3});
cout<<“vector of pairs, vec4 : “<<endl;
for(auto i: vec4){
cout<<i.first<<” “<<i.second<<endl;
}
return 0;
}
Output
vec1: 1 2 3 4 5 6
vec2: 0 0
vec1 after erasing: 1 2 3 4 6
vec3:-
1 1 1 1
1 1 1 1
vector of pairs, vec4 :
2 3
4 3
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 −
S.No.
Function
Function Explanation
TC