STL in C++
C++ provides a collection of ready-made containers, functions, and tools that make common programming tasks much easier. This collection is called the Standard Template Library (STL).
Instead of creating everything from scratch, you can use STL containers to store data and STL algorithms to work with that data.
In this tutorial, we will look at some commonly used STL Containers in C++, iterators, and algorithms.
What is STL in C++?
STL in C++ is a part of the C++ Standard Library that provides reusable components for handling data.
The main parts you will commonly use are:
- Containers – store data
- Iterators – move through container elements
- Algorithms – perform operations on data
For example, if you need to store a list of numbers, you can use a vector instead of creating an array manually.
vector
vector is one of the most commonly used containers in C++. It stores multiple values and can automatically grow or shrink when needed.
#include <vector>
vector<int> numbers = {10, 20, 30};
numbers.push_back(40);
cout << numbers[0];
Output
10
You can use size() to find the number of elements:
cout << numbers.size();
A vector is similar to an array, but its size can change during program execution. This makes C++ STL Vector useful in many programs.
string
C++ provides the string class for working with text.
string name = "Aman";
cout << name;
You can easily perform operations such as finding the length:
cout << name.length();
Strings can also be modified using functions such as append(), substr(), and find().
pair
pair is used to store two related values together.
pair<string, int> student;
student.first = "Aman";
student.second = 85;
cout << student.first << endl;
cout << student.second;
Here, first stores the name and second stores the marks.
set
A set stores unique values in sorted order.
set<int> numbers = {30, 10, 20, 10};
for (int n : numbers)
{
cout << n << " ";
}
Output
10 20 30
The duplicate 10 is stored only once.
map
A map stores data in key-value pairs.
map<string, int> marks;
marks["Aman"] = 85;
marks["Riya"] = 92;
cout << marks["Aman"];
Output
85
A map is useful when you want to find a value using a key.
unordered_set and unordered_map
unordered_set stores unique values but does not maintain them in sorted order.
unordered_set<int> numbers = {30, 10, 20};
unordered_map stores key-value pairs without maintaining the keys in sorted order.
unordered_map<string, int> marks;
marks["Aman"] = 85;
marks["Riya"] = 92;
These containers are commonly used when fast average-time lookup is more important than maintaining sorted order.
stack
A stack follows LIFO, which means Last In, First Out.
stack<int> s;
s.push(10);
s.push(20);
s.push(30);
cout << s.top();
Output
30
The last value added is the first value removed.
queue
A queue follows FIFO, which means First In, First Out.
queue<int> q;
q.push(10);
q.push(20);
q.push(30);
cout << q.front();
Output
10
The first value added is the first one processed.
priority_queue
A priority_queue gives priority to the largest element by default.
priority_queue<int> pq;
pq.push(10);
pq.push(30);
pq.push(20);
cout << pq.top();
Output
30
It is useful when you need to process the highest-priority value first.
Iterators
Iterators are used to move through the elements of STL containers.
For example:
vector<int> numbers = {10, 20, 30};
for (vector<int>::iterator it = numbers.begin();
it != numbers.end(); it++)
{
cout << *it << " ";
}
Output
10 20 30
begin() points to the first element, while end() represents the position after the last element.
STL Algorithms in C++
C++ provides many ready-made algorithms for common operations.
For example, sort() sorts the elements:
vector<int> numbers = {30, 10, 20};
sort(numbers.begin(), numbers.end());
After sorting:
10 20 30
find() searches for a value:
auto it = find(numbers.begin(), numbers.end(), 20);
Other useful algorithms include:
binary_search()– checks whether an element exists in sorted datalower_bound()– finds the first position where a value can be insertedupper_bound()– finds the position after the last matching valuereverse()– reverses a rangemax_element()– finds the largest elementmin_element()– finds the smallest element
These algorithms save time because you don’t need to write the same searching and sorting logic yourself.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
