The Standard Template Library (STL) is one of the most powerful features of modern C++. It is a massive collection of pre-written, highly optimized template classes and functions that provide common data structures and algorithms.
Instead of writing a Linked List, a dynamic array, or a sorting algorithm from scratch, you can simply use the STL.
The STL is built around three core components that work together seamlessly:
Containers manage the storage space for their elements. They are implemented as class templates, which means they can hold any data type.
Common containers include:
vector: A dynamic array that automatically resizes itself.list: A doubly-linked list.map: A collection of key-value pairs (like a dictionary), automatically sorted by keys.set: A collection of unique elements, automatically sorted.queue / stack: Standard FIFO and LIFO structures.std::vector#include <iostream>
#include <vector>
using namespace std;
int main() {
// Create a vector of integers
vector<int> numbers;
// Add elements to the end
numbers.push_back(10);
numbers.push_back(20);
numbers.push_back(30);
cout << "Size of vector: " << numbers.size() << endl;
// Access elements like a normal array
cout << "Element at index 1: " << numbers[1] << endl;
return 0;
}Iterators are like advanced pointers. They are used to point at the memory addresses of STL container elements. They act as a bridge between containers and algorithms.
Most containers provide begin() (points to the first element) and end() (points to one past the last element).
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector<string> names = {"Alice", "Bob", "Charlie"};
// Creating an iterator for the vector
vector<string>::iterator it;
// Looping through the vector using the iterator
for (it = names.begin(); it != names.end(); ++it) {
cout << *it << " "; // Dereference the iterator to get the value
}
return 0;
}The <algorithm> header provides dozens of functions that operate on ranges of elements (defined by iterators).
#include <iostream>
#include <vector>
#include <algorithm> // Required for STL algorithms
using namespace std;
int main() {
vector<int> nums = {50, 10, 40, 20, 30};
// Sort the vector in ascending order
sort(nums.begin(), nums.end());
// Reverse the sorted vector
reverse(nums.begin(), nums.end());
// Print results (Using C++11 range-based for loop for simplicity)
for (int num : nums) {
cout << num << " ";
}
// Output: 50 40 30 20 10
vector, map, list).sort, find, reverse).A powerful library of template classes and functions providing pre-built data structures and algorithms.
Objects that store collections of data (e.g., vector, list, map).
Objects that act like pointers, allowing you to traverse through the elements of a container.
Functions that perform operations on containers (e.g., sort, search, reverse).
Test your understanding with 2 questions
Which of the following is NOT a core component of the STL?
Which STL container provides a dynamic array that can grow in size automatically?
2 Modules
2 Modules