The STL is the backbone of every C++ program. It provides three core components:
Containers — data structures (vector, map, set, deque, list, etc.)
Iterators — uniform way to traverse any container
Algorithms — sort, search, transform, and more — work on any container via iterators
All STL components live in the std namespace and are included via #include <header>.
Sequence Containers
std::vector — Dynamic Array
#include <vector>std::vector<int> v = {1, 2, 3, 4, 5};v.push_back(6); // add to endv.pop_back(); // remove from endv.insert(v.begin(), 0); // insert at positionv.erase(v.begin() + 2); // erase at positionv.clear(); // remove allstd::cout << v.size(); // number of elementsstd::cout << v.capacity(); // allocated memorystd::cout << v[0]; // access (no bounds check)std::cout << v.at(0); // access (throws if out of range)std::cout << v.front(); // first elementstd::cout << v.back(); // last elementv.reserve(100); // pre-allocate memory (avoids reallocations)v.resize(10); // resize (fills with 0 if growing)v.shrink_to_fit(); // release unused capacity// Iteratefor (const auto& x : v) std::cout << x << " ";
std::deque — Double-Ended Queue
#include <deque>std::deque<int> dq = {2, 3, 4};dq.push_front(1); // add to front — O(1)dq.push_back(5); // add to back — O(1)dq.pop_front(); // remove frontdq.pop_back(); // remove backstd::cout << dq[0]; // random access — O(1)std::cout << dq.size(); // 3// Use when you need fast insert/remove at both ends
std::list — Doubly Linked List
#include <list>std::list<int> lst = {1, 2, 3, 4, 5};lst.push_front(0);lst.push_back(6);lst.pop_front();lst.pop_back();// Insert at iterator position — O(1)auto it = lst.begin();std::advance(it, 2);lst.insert(it, 99);lst.remove(99); // remove all elements equal to 99lst.sort(); // sort in-placelst.reverse(); // reverse in-placelst.unique(); // remove consecutive duplicates// No random access — must iterate
std::array — Fixed-Size Array (C++11)
#include <array>std::array<int, 5> arr = {1, 2, 3, 4, 5};std::cout << arr[0]; // 1std::cout << arr.size(); // 5 (compile-time constant)std::cout << arr.front(); // 1std::cout << arr.back(); // 5arr.fill(0); // set all to 0// Prefer over C arrays — same performance, bounds-safe with .at()
std::forward_list — Singly Linked List (C++11)
#include <forward_list>std::forward_list<int> fl = {1, 2, 3};fl.push_front(0);fl.pop_front();fl.insert_after(fl.begin(), 99); // insert after iteratorfl.remove(99);fl.sort();// Minimal memory overhead — no size() method
#include <unordered_map>std::unordered_map<std::string, int> umap;umap["x"] = 10;umap["y"] = 20;umap.insert({"z", 30});std::cout << umap["x"]; // 10std::cout << umap.count("y"); // 1umap.erase("x");// Average O(1) lookup vs O(log n) for std::map// No guaranteed order// Reserve buckets to avoid rehashingumap.reserve(1000);
std::set — Sorted Unique Elements
#include <set>std::set<int> s = {5, 3, 1, 4, 2, 3}; // duplicates removed// s = {1, 2, 3, 4, 5}s.insert(6);s.erase(3);std::cout << s.count(4); // 1 (exists) or 0 (not found)std::cout << s.size(); // 5auto it = s.find(4);if (it != s.end()) std::cout << *it; // 4// lower_bound / upper_boundauto lb = s.lower_bound(3); // first element >= 3auto ub = s.upper_bound(3); // first element > 3
std::unordered_set — Hash Set (C++11)
#include <unordered_set>std::unordered_set<int> us = {1, 2, 3, 4, 5};us.insert(6);us.erase(3);std::cout << us.count(4); // 1 or 0// O(1) average lookup, no order
#include <span>void process(std::span<int> data) { for (int x : data) std::cout << x << " ";}std::vector<int> v = {1, 2, 3, 4, 5};process(v); // works with vectorint arr[] = {6, 7, 8};process(arr); // works with C arrayprocess(std::span(v).subspan(1, 3)); // {2, 3, 4}
Container Complexity Summary
Container Access Insert(end) Insert(mid) Search
vector O(1) O(1) amort. O(n) O(n)
deque O(1) O(1) O(n) O(n)
list O(n) O(1) O(1)* O(n)
array O(1) N/A N/A O(n)
map O(log n) O(log n) O(log n) O(log n)
unordered_map O(1) avg O(1) avg O(1) avg O(1) avg
set N/A O(log n) O(log n) O(log n)
unordered_set N/A O(1) avg O(1) avg O(1) avg
stack/queue O(1) O(1) N/A N/A
priority_queue O(1) top O(log n) N/A N/A
* list insert is O(1) given an iterator to the position