Intermediate⏱️ 11 min📘 Topic 31 of 32

📚 C++ STL Introduction — vector, map, set and Iterators

Beginner's guide to the C++ Standard Template Library (STL) — vector, map, set, iterators, and common algorithms (sort, find, count). With examples and interview Q&A.

The STL (Standard Template Library) is C++'s built-in toolkit of generic containers, iterators, and algorithms. Knowing it is half the language.

🧺 Containers (the ones you'll use 90%)

  • std::vector<T> — dynamic array. Default choice.
  • std::map<K,V> — sorted key→value (O(log n))
  • std::unordered_map<K,V> — hash map (O(1) average)
  • std::set<T> — sorted unique elements
  • std::unordered_set<T> — hash set
  • std::pair, std::tuple — fixed-size bundles
  • std::stack, std::queue, std::priority_queue

🔁 Iterators

std::vector<int> v = {3,1,4,1,5};
for (auto it = v.begin(); it != v.end(); ++it) {
  std::cout << *it;
}

⚡ Algorithms (from <algorithm>)

  • std::sort
  • std::find
  • std::count
  • std::reverse
  • std::min_element, std::max_element
  • std::accumulate (from <numeric>)

💻 Code Examples

vector: the workhorse

#include <vector>
#include <algorithm>
std::vector<int> nums = {3,1,4,1,5,9,2,6};
nums.push_back(7);
std::sort(nums.begin(), nums.end());
for (int n : nums) std::cout << n << ' ';
Output:
1 1 2 3 4 5 6 7 9

map: word counter

#include <map>
#include <string>
std::map<std::string, int> count;
std::string words[] = {"a","b","a","c","b","a"};
for (auto& w : words) count[w]++;
for (auto& [k, v] : count) std::cout << k << ':' << v << ' ';
Output:
a:3 b:2 c:1

set: unique values, auto-sorted

#include <set>
std::set<int> s = {5, 2, 8, 2, 5, 1};
for (int x : s) std::cout << x << ' ';
Output:
1 2 5 8

⚠️ Common Mistakes

  • Using `std::list` when `std::vector` would do — vectors are almost always faster.
  • Using `std::map` when ordering isn't needed — `std::unordered_map` is faster.
  • Capturing iterators across `push_back` — vectors may reallocate, invalidating iterators.
  • Forgetting `#include <algorithm>` or `<numeric>` when using sort, accumulate etc.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What is std::vector and how is it different from a C array?Beginner
vector is a dynamic, resizable array. Knows its size, automatic memory management, can grow with push_back, supports iterators and STL algorithms.
Q2.When would you choose std::map vs std::unordered_map?Intermediate
map: sorted (red-black tree), O(log n) operations. unordered_map: hash table, O(1) average. Default to unordered_map unless you need ordered traversal.
Q3.What is an iterator?Intermediate
A generalization of a pointer — an object that points to an element in a container and can be advanced (++it), dereferenced (*it) and compared.
Q4.What's the complexity of std::sort?Intermediate
O(n log n) average and worst case. Typically implemented as introsort.
Q5.Why is std::vector cache-friendly?Advanced
Elements are stored in CONTIGUOUS memory. The CPU prefetches nearby data into cache, so iterating a vector hits the cache almost every access.

🧠 Quick Summary

  • STL = containers + iterators + algorithms.
  • Default container: std::vector.
  • Use unordered_map for fast key lookup, map when order matters.
  • Range-for hides iterators most of the time.
  • Algorithms are generic — `std::sort` works on any random-access range.