Introduction to STL in C++
In this lesson: Introduction to the C++ Standard Template Library — containers, iterators, algorithms — beginner guide with code. In Hindi & English.
What is the STL?
Suppose you need to store marks of any number of students, sort them, and find the highest. In C you would write your own dynamic array, your own sort and your own search — and debug all three. The Standard Template Library (STL) gives you all of this ready-made, tested and fast:
std::vector<int> marks = {78, 91, 66, 84};
std::sort(marks.begin(), marks.end());The STL is a collection of templates — generic code that works with any data type — for data structures and algorithms. It was designed by Alexander Stepanov and became part of standard C++ in 1998. This lesson is a first overview; for a deeper tour see the C++ STL guide.
The four components
| Component | What it does | Examples |
|---|---|---|
| Containers | Store a collection of objects | vector, list, deque, set, map, unordered_map |
| Iterators | Point to elements and move through a container | begin(), end(), ++it, *it |
| Algorithms | Process ranges of elements | sort, find, count, reverse, max_element |
| Function objects / lambdas | Customise how algorithms behave | std::greater<int>(), [](int a, int b){ return a > b; } |
The key design idea: algorithms don't know about containers. They work on a pair of iterators (a range), so one sort works on a vector, a deque or a plain array.
First example: store, sort, search
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> marks = {78, 91, 66, 84, 91, 55};
marks.push_back(73); // container grows
std::sort(marks.begin(), marks.end()); // algorithm
std::cout << "Sorted: ";
for (int m : marks) std::cout << m << " ";
std::cout << "\n";
auto best = std::max_element(marks.begin(), marks.end()); // iterator
std::cout << "Highest: " << *best << "\n";
int toppers = std::count(marks.begin(), marks.end(), 91);
std::cout << "Students with 91: " << toppers << "\n";
auto it = std::find(marks.begin(), marks.end(), 66);
if (it != marks.end())
std::cout << "66 found at position " << (it - marks.begin()) << "\n";
return 0;
}No manual memory management, no hand-written sorting loop — and each line says what it does.
Types of containers
Sequence containers — elements in the order you put them
- [
vector](/tutorial/cpp/vector/) — dynamic array; fast access by index, fast add/remove at the end. - [
deque](/tutorial/cpp/list-deque/) — double-ended queue; fast add/remove at both ends. - [
list](/tutorial/cpp/list-deque/) — doubly linked list; fast insert/erase anywhere once you have an iterator. array— fixed-size array with STL features.forward_list— singly linked list.
Associative containers — kept sorted by key
- [
set/multiset](/tutorial/cpp/set-map/) — sorted values (unique / duplicates allowed). - [
map/multimap](/tutorial/cpp/set-map/) — sorted key → value pairs.
Unordered containers — hash tables
unordered_set,unordered_map— average O(1) lookup, no ordering.
Container adapters — restricted interfaces
- [
stack](/tutorial/cpp/stack-queue/) (LIFO), [queue](/tutorial/cpp/stack-queue/) (FIFO), [priority_queue](/tutorial/cpp/stack-queue/) (largest first).
Which container should I use?
| You need… | Use |
|---|---|
| A list of items, mostly added at the end, accessed by index | vector |
| Fast insert/remove at the front and back | deque |
| Unique items kept sorted (e.g. roll numbers) | set |
| Look up a value by key, in sorted key order (e.g. name → marks) | map |
| Fastest lookup by key, order doesn't matter | unordered_map |
| Last-in-first-out (undo, bracket matching) | stack |
| First-in-first-out (token queue, BFS) | queue |
| Always take the highest/lowest priority next | priority_queue |
When in doubt, start with vector.
A second example: map
#include <iostream>
#include <map>
#include <string>
int main() {
std::map<std::string, int> marks;
marks["Priya"] = 91;
marks["Aman"] = 78;
marks["Ravi"] = 66;
for (const auto& [name, m] : marks) // C++17 structured binding
std::cout << name << " -> " << m << "\n";
return 0;
}The map keeps names in alphabetical order automatically.
Why use the STL?
- Correct: used by millions of programmers and tested by compiler vendors.
- Fast: algorithms have guaranteed complexity, e.g.
sortis O(n log n). - Generic: the same
vectorworks forint,std::stringor your ownStudentclass. - Less code: fewer loops and no manual
new/delete. - Required knowledge: used in almost every C++ interview, competitive programming and real project.
Common mistakes
- Forgetting the header:
<vector>,<map>,<algorithm>must be included separately. - Dereferencing
end()— it points *past* the last element, not to it. - Using
findon amap's range instead ofmap.find(key)(much slower). - Modifying a
vector(e.g.push_back) while holding iterators to it — they may become invalid. - Using
operator[]on amapjust to check if a key exists; it inserts the key. Usefindorcount.
Read 8 marks into a vector<int>. Print them sorted in descending order (hint: std::greater<int>()), print the average using std::accumulate from <numeric>, and count how many are 33 or above using std::count_if with a lambda.
Summary
- The STL provides ready-made containers, iterators, algorithms and function objects.
- Algorithms work on iterator ranges, so they are independent of the container.
- Sequence, associative, unordered and adapter containers each suit different jobs.
- Start with
vector; usemap/unordered_mapfor key-based lookup. - Include the right header and never dereference
end().
Next: vector → list and deque → stack and queue → set and map → iterators → algorithms.
Frequently Asked Questions
What is STL in C++?
vector, list, map, set) and algorithms (like sort, find, count) that work together through iterators.What are the components of STL?
stack and queue are built on top of containers.What are the types of containers in STL?
vector, deque, list, forward_list, array. Associative containers keep elements sorted by key: set, multiset, map, multimap. Unordered containers use hashing: unordered_set, unordered_map. Container adapters: stack, queue, priority_queue.Which STL container should I use by default?
std::vector unless you have a reason not to. It stores elements contiguously, is cache-friendly, supports fast random access and fast insertion at the end. Choose map/unordered_map for key–value lookups, set for unique sorted values, and deque when you need fast insertion at both ends.