So far, we have written many things ourselves.
But C++ already provides many useful tools that we can use instead of creating everything from scratch.
These tools are part of the STL, which stands for Standard Template Library.
C++ STL gives us ready-made containers and functions that make programming easier and faster.
What is STL in C++?
STL is a collection of ready-to-use tools in C++.
It includes:
- Containers
- Iterators
- Algorithms
For example, if you want to store many numbers, you can use a vector instead of creating everything yourself.
STL
├── Containers
├── Iterators
└── Algorithms
STL is used in many real C++ programs.
C++ STL Containers
A container is used to store and organize data.
Some commonly used STL containers are:
vectorliststackqueuesetmap
Each container works differently.
For example:
vector → Stores items in a sequence
stack → Last item comes out first
queue → First item comes out first
set → Stores unique values
map → Stores key-value pairs
Let’s look at them one by one.
C++ STL Iterators
An iterator is used to move through the elements of an STL container.
Think of an iterator like a pointer that helps us move from one item to another.
For example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {10, 20, 30, 40, 50};
vector<int>::iterator it;
for (it = numbers.begin(); it != numbers.end(); ++it) {
cout << *it << "\n";
}
return 0;
}
Output:
10
20
30
40
50
Here:
numbers.begin()
points to the first element.
And:
numbers.end()
points to the position just after the last element.
The * is used to get the value at the iterator’s current position.
C++ STL Algorithms
STL also provides ready-made algorithms for common tasks.
For example, we can use sort() to sort numbers.
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
vector<int> numbers = {50, 10, 40, 20, 30};
sort(numbers.begin(), numbers.end());
cout << "Sorted numbers:\n";
for (int number : numbers) {
cout << number << " ";
}
return 0;
}
Output:
Sorted numbers:
10 20 30 40 50
Instead of writing our own sorting logic, we can simply use:
sort(numbers.begin(), numbers.end());
This is one of the useful features of STL.
vector
A vector is a container that stores multiple values.
It is similar to an array, but a vector can grow or shrink when needed.
To use a vector, include:
#include <vector>
Let’s see a complete example:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers;
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
cout << "Numbers in the vector:\n";
for (int number : numbers) {
cout << number << "\n";
}
return 0;
}
Output:
Numbers in the vector:
10
20
30
The function:
numbers.push_back(10);
adds 10 to the vector.
We can add as many values as we need.
Getting the Size of a Vector
We can use size() to find how many elements are stored.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> fruits;
fruits.push_back("Apple");
fruits.push_back("Mango");
fruits.push_back("Banana");
cout << "Number of fruits: " << fruits.size() << "\n";
cout << "Fruits:\n";
for (string fruit : fruits) {
cout << fruit << "\n";
}
return 0;
}
Output:
Number of fruits: 3
Fruits:
Apple
Mango
Banana
list
A list stores multiple elements like a sequence.
One useful feature of a list is that adding or removing elements from the beginning or middle can be efficient.
To use it, include:
#include <list>
Example:
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> numbers;
numbers.push_back(20);
numbers.push_back(30);
numbers.push_front(10);
cout << "Numbers in the list:\n";
for (int number : numbers) {
cout << number << " ";
}
return 0;
}
Output:
Numbers in the list:
10 20 30
Here:
numbers.push_back(20);
adds an element at the end.
And:
numbers.push_front(10);
adds an element at the beginning.
stack
A stack works like a stack of plates.
Imagine putting plates one on top of another.
The last plate you put on is the first plate you take off.
This is called LIFO:
Last In, First Out
To use a stack:
#include <stack>
Example:
#include <iostream>
#include <stack>
using namespace std;
int main() {
stack<int> numbers;
numbers.push(10);
numbers.push(20);
numbers.push(30);
cout << "Top element: " << numbers.top() << "\n";
numbers.pop();
cout << "Top element after pop: " << numbers.top();
return 0;
}
Output:
Top element: 30
Top element after pop: 20
Here:
numbers.push(30);
puts 30 on top.
And:
numbers.pop();
removes the top element.
queue
A queue works like a line of people waiting at a ticket counter.
The person who comes first gets served first.
This is called FIFO:
First In, First Out
To use a queue:
#include <queue>
Example:
#include <iostream>
#include <queue>
using namespace std;
int main() {
queue<string> students;
students.push("Rahul");
students.push("Aman");
students.push("Priya");
cout << "First student: " << students.front() << "\n";
students.pop();
cout << "Next student: " << students.front();
return 0;
}
Output:
First student: Rahul
Next student: Aman
Here:
students.push("Rahul");
adds Rahul to the queue.
And:
students.pop();
removes the first student.
set
A set stores values where duplicate values are not allowed.
For example, if we add 10 twice, the set will keep only one 10.
To use a set:
#include <set>
Example:
#include <iostream>
#include <set>
using namespace std;
int main() {
set<int> numbers;
numbers.insert(10);
numbers.insert(20);
numbers.insert(10);
numbers.insert(30);
cout << "Numbers in the set:\n";
for (int number : numbers) {
cout << number << " ";
}
return 0;
}
Output:
Numbers in the set:
10 20 30
Notice that 10 was added twice, but it appears only once.
A set also keeps its elements sorted by default.
map
A map stores data in key-value pairs.
Think of a dictionary.
You have:
Word → Meaning
In a C++ map, you can have:
Key → Value
For example:
Name → Age
Rahul → 14
Priya → 15
To use a map:
#include <map>
Example:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> students;
students["Rahul"] = 14;
students["Priya"] = 15;
students["Aman"] = 13;
cout << "Rahul's age: " << students["Rahul"] << "\n";
cout << "Priya's age: " << students["Priya"];
return 0;
}
Output:
Rahul's age: 14
Priya's age: 15
Here:
students["Rahul"] = 14;
means:
Key → Rahul
Value → 14
We can also display all the values:
#include <iostream>
#include <map>
using namespace std;
int main() {
map<string, int> students;
students["Rahul"] = 14;
students["Priya"] = 15;
students["Aman"] = 13;
cout << "Student Information:\n";
for (auto student : students) {
cout << student.first << " - " << student.second << "\n";
}
return 0;
}
Output:
Student Information:
Aman - 13
Priya - 15
Rahul - 14
Here:
student.first
gives the key.
And:
student.second
gives the value.
Quick Comparison of C++ STL Containers
| Container | Main Use |
|---|---|
vector | Store values in a dynamic sequence |
list | Store elements in a linked sequence |
stack | Last In, First Out |
queue | First In, First Out |
set | Store unique values |
map | Store key-value pairs |
Why Use C++ STL?
STL saves us from writing common data structures and algorithms from scratch.
For example, instead of creating our own dynamic array, we can use:
vector<int>
Instead of writing our own sorting algorithm, we can use:
sort()
This makes C++ programs easier and faster to write.
Key Points to Remember
- STL stands for Standard Template Library.
- STL provides ready-to-use containers, iterators, and algorithms.
vectorstores a dynamic collection of values.liststores elements in a linked sequence.stackfollows LIFO.queuefollows FIFO.setstores unique values.mapstores key-value pairs.- Iterators help us move through containers.
- Algorithms provide ready-made operations such as sorting.
Frequently Asked Questions (FAQs)
Q1. What is C++ STL used for?
C++ STL provides ready-to-use containers, iterators, and algorithms, helping programmers handle common programming tasks without building everything from scratch.
Q2. What are C++ STL Containers?
C++ STL containers are ready-made data structures used to store and organize data. Common examples include vector, list, stack, queue, set, and map.
Q3. When should you use a C++ vector?
C++ vector is useful when you need a collection that can dynamically grow or shrink while storing elements in a sequence.
Q4. What is the difference between a stack and queue in C++?
A C++ stack follows LIFO (Last In, First Out), while a queue follows FIFO (First In, First Out).
Q5. What are map and set used for in C++?
A set stores unique values, while a map stores data in key-value pairs, making them useful for different types of data organization.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
