-
Hajipur, Bihar, 844101
C++ is one of the most powerful programming languages for handling complex data and large-scale applications. A big reason behind its flexibility is the Standard Template Library (STL) — a collection of ready-made classes and functions that make working with data structures and algorithms easier.
In this tutorial, you’ll learn what data structures are, how STL is organized, and how it simplifies programming through reusable, generic components.
A data structure is a systematic way to organize, manage, and store data efficiently. It determines how quickly data can be accessed, modified, and processed.
When you deal with large amounts of data, choosing the right data structure can make your program faster and more memory-efficient.
C++ supports various built-in and user-defined data structures:
Arrays – Used to store a fixed-size collection of similar elements.
Linked Lists – A sequence of nodes where each node points to the next.
Stacks – Follow the Last In, First Out (LIFO) rule, where the last added item is removed first.
Queues – Follow the First In, First Out (FIFO) rule, where the first item added is the first to be removed.
Trees – Store elements hierarchically, making searching and sorting efficient.
Graphs – Represent networks or relationships between elements using nodes and edges.
Implementing these structures manually can be time-consuming. This is where STL becomes essential — it provides predefined templates for many commonly used data structures.
The Standard Template Library (STL) is a collection of C++ templates that provide data structures and algorithms for handling data efficiently. It saves developers from writing repetitive code and helps maintain consistency across applications.
STL has four major components:
Containers – Store collections of objects or data.
Iterators – Provide a way to access elements of containers.
Algorithms – Offer operations such as sorting, searching, and merging.
Function Objects (Functors) – Let you use functions like objects for flexible computation.
Containers are the core of STL. They hold data and manage memory automatically, making programs cleaner and safer. Each type of container serves a specific purpose.
These store data in a linear order.
Vector – A dynamic array that automatically resizes when new elements are added.
Deque (Double-Ended Queue) – Allows insertion and deletion from both ends.
List – A doubly linked list that allows insertion and deletion anywhere efficiently.
Example using vector:
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {10, 20, 30, 40};
nums.push_back(50);
for (int n : nums)
cout << n << " ";
return 0;
}
Output:
10 20 30 40 50
These store elements in a sorted structure and use keys for fast searching.
Set – Stores unique values in sorted order.
Map – Stores key-value pairs, each key being unique.
Multiset and Multimap – Allow duplicate elements or keys.
These are based on hash tables, allowing faster average-time lookups.
unordered_set
unordered_map
unordered_multiset
unordered_multimap
Iterators work as pointers that move through container elements. They help you traverse and manipulate data in a consistent way, regardless of container type.
begin() – Points to the first element.
end() – Points one position past the last element.
rbegin() and rend() – Used for reverse traversal.
const_iterator – Used when you only want to read values.
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<int> numbers = {5, 10, 15};
vector<int>::iterator it;
for (it = numbers.begin(); it != numbers.end(); ++it)
cout << *it << " ";
return 0;
}
Output:
5 10 15
One of the most powerful parts of STL is its algorithm library. It includes over 100 predefined algorithms that perform common tasks like sorting, searching, and counting.
sort(begin, end) – Sorts elements in ascending order.
reverse(begin, end) – Reverses the order of elements.
count(begin, end, value) – Counts how many times a value appears.
find(begin, end, value) – Finds a specific value in a range.
max_element(begin, end) – Returns the largest element.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main() {
vector<int> nums = {8, 2, 5, 1};
sort(nums.begin(), nums.end());
for (int n : nums)
cout << n << " ";
return 0;
}
Output:
1 2 5 8
A functor is an object that behaves like a function. It’s used to modify how algorithms process data.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Square {
void operator()(int x) const {
cout << x * x << " ";
}
};
int main() {
vector<int> nums = {1, 2, 3, 4};
for_each(nums.begin(), nums.end(), Square());
return 0;
}
Output:
1 4 9 16
Functors are especially useful when you need to apply custom logic inside STL algorithms.
Efficiency – STL components are optimized for performance.
Reusability – Templates work with any data type.
Reduced Complexity – Predefined structures simplify logic.
Reliability – Built-in code minimizes chances of bugs.
Scalability – Works efficiently even for large datasets.
The C++ Standard Template Library (STL) is a complete toolkit that brings together data structures, algorithms, and utilities to make development faster and cleaner. Containers store data efficiently, iterators make navigation easy, and algorithms handle operations quickly.
Once you understand STL, you can write flexible, reliable, and high-performance C++ programs without reinventing basic data-handling logic. It’s one of the most practical tools every modern C++ programmer should master.
Write a program that creates a vector of integers, adds 5 elements, and displays them using an iterator.
Create a list of strings, insert names in alphabetical order, and display the list.
Write a program that stores unique roll numbers in a set and checks if a given roll number exists.
Build a program that uses a map to store student names and their marks, then prints all entries.
Write a program using an unordered_map to store product names as keys and prices as values. Display all products.
Create a program that reads 10 integers into a vector and uses sort() and reverse() to display them in descending order.
Write a program using count() to count how many times a given value appears in a vector.
Create a program that uses find() to search for a number in a list and displays whether it exists or not.
Write a program that demonstrates a functor to square each element in a vector using for_each().
Build a program using multimap to store multiple students having the same grade and display all key-value pairs.