What is the STL in C++?
The STL, or Standard Template Library, is a collection of ready made template classes and functions in C++. It has three main parts: containers like vector and map that store data, algorithms like sort and find that operate on data, and iterators that connect the two by walking through containers. It saves you from writing common data structures and algorithms yourself.
- The STL is a library of ready made template containers, algorithms, and iterators.
- Containers store data, algorithms process it, and iterators connect the two.
- It saves you from writing common data structures and algorithms by hand.
The three main parts
- Containers: vector, list, map, set, and more for storing data.
- Algorithms: sort, find, count, and others that process data.
- Iterators: objects that point into a container so algorithms can traverse it.
#include <vector>
#include <algorithm>
std::vector<int> v = {3, 1, 2};
std::sort(v.begin(), v.end()); // container + algorithm + iterators
A common follow up is which container you would use for a task. Practice quick choices like vector for a dynamic array, map for key lookups, and set for unique sorted values. Matching container to need is the practical skill.
Frequently asked questions
What is an iterator?
An iterator is an object that points into a container and can move through its elements, letting algorithms work with any container in a uniform way.
When would you use a map over an unordered_map?
Use map when you need keys kept in sorted order, and unordered_map when you only need fast average lookups and order does not matter.
Common follow up questions
Related interview questions
Want the full C++ guide?
Read every C++ concept with notes, diagrams, and code in one place. Track your progress as you go.
Open the C++ guide All C++ questions