What is the Standard Template Library (STL)?
Why Interviewers Ask This
Interviewers use this question to quickly assess whether a candidate has the foundational knowledge required for C++ development. It reveals whether you understand the building blocks that more complex concepts rely on.
Answer
The STL (Standard Template Library) is a collection of generic, reusable components in C++ — containers, algorithms, and iterators — that work together through a unified interface. Containers: Sequence: vector (dynamic array, O(1) access, O(n) insert middle), deque (double-ended queue), list (doubly linked, O(1) insert anywhere, no random access), array (fixed-size stack array); Associative (sorted): set (unique keys), map (key-value, sorted), multiset, multimap — all O(log n) via red-black tree; Unordered (hash): unordered_set, unordered_map — O(1) average, O(n) worst; Adaptors: stack, queue, priority_queue. Algorithms (from <algorithm>): std::sort(v.begin(), v.end()); std::find(v.begin(), v.end(), 5); std::count_if(v.begin(), v.end(), [](int x){ return x > 0; }); std::transform(v.begin(), v.end(), out.begin(), [](int x){ return x*2; }); std::accumulate(v.begin(), v.end(), 0); std::binary_search(v.begin(), v.end(), 5); std::max_element(v.begin(), v.end());. Iterators: generalized pointers that abstract container traversal. for (auto it = v.begin(); it != v.end(); ++it) *it *= 2;. Or range-based for: for (auto& x : v) x *= 2;. Why STL matters: type-safe, efficient (hand-optimized), consistent interface across containers, composable with algorithms — the backbone of modern C++ development.
Pro Tip
If you're unsure about a detail, say so honestly and explain your reasoning. Interviewers respect candidates who can think through uncertainty rather than bluffing.