What are STL Containers in C++?
Learn what STL containers are in C++ — vector, list, map, set and unordered_map — their categories, complexity, and how to choose the right one.
Expected Interview Answer
STL containers are template-based classes in the C++ Standard Library that store and manage collections of objects, such as vector, list, map, set, and unordered_map, each with defined performance characteristics.
They are grouped into sequence containers (vector, deque, list) that keep elements in a linear order, associative containers (map, set) that keep keys sorted via balanced trees, and unordered containers (unordered_map, unordered_set) that use hashing for average O(1) access. Containers own their elements, manage memory automatically, and expose a uniform interface of iterators so the same algorithms work across them.
- Reusable, type-safe generic storage via templates
- Automatic memory management (no manual new/delete)
- Documented time complexity for each operation
- Uniform iterator interface usable by STL algorithms
- Reduces bugs from hand-rolled data structures
AI Mentor Explanation
Think of STL containers as the different fielding formations a captain keeps ready: a vector is like players lined up in a set batting order you can index instantly, a map is like a sorted roster looked up by jersey number, and a queue is like batsmen padded up waiting their turn to walk out.
Step-by-Step Explanation
Step 1
Pick the container category
Decide between sequence (vector, deque, list), associative (map, set), or unordered (hash-based) containers based on access pattern.
Step 2
Match access needs
Use vector for indexed random access, list for cheap mid-sequence insertions, map/set for sorted keyed lookup, unordered_map for average O(1) keyed lookup.
Step 3
Consider complexity
Weigh time complexity: vector index is O(1) but middle insert is O(n); map operations are O(log n); unordered_map is average O(1).
Step 4
Use iterators
Traverse and manipulate elements through the container's uniform iterator interface so STL algorithms apply.
Step 5
Let RAII manage memory
Rely on the container to allocate and free storage automatically as elements are added and removed.
What Interviewer Expects
- Knowledge of the three container categories
- Time complexity of common operations per container
- When to choose vector vs list vs map vs unordered_map
- Understanding that containers own and manage memory
- Awareness of iterators as the common interface
Common Mistakes
- Defaulting to vector for every problem regardless of access pattern
- Confusing map (sorted, tree) with unordered_map (hashed)
- Assuming list gives O(1) random access like vector
- Ignoring iterator invalidation after insert or erase
- Not knowing the complexity guarantees of chosen operations
Best Answer (HR Friendly)
“STL containers are ready-made building blocks in C++ for storing groups of data, like lists, sorted maps, and sets. They handle memory for you and each is tuned for a different job, so developers pick the right one instead of writing their own from scratch.”
Code Example
#include <vector>
#include <map>
#include <set>
#include <iostream>
int main() {
std::vector<int> scores{85, 90, 78};
scores.push_back(95); // O(1) amortized append
std::cout << scores[1] << "\n"; // O(1) index access -> 90
std::map<std::string, int> ages; // sorted by key, O(log n)
ages["alice"] = 30;
ages["bob"] = 25;
std::set<int> unique{3, 1, 2, 1}; // dedups, stays sorted: {1,2,3}
unique.insert(2); // no effect, already present
for (const auto& [name, age] : ages)
std::cout << name << ": " << age << "\n";
return 0;
}Follow-up Questions
- When would you choose std::deque over std::vector?
- What is the difference between map and unordered_map?
- How does iterator invalidation differ between vector and list?
- What are container adapters like stack, queue, and priority_queue?
- Why is vector usually preferred over list despite O(n) middle inserts?
MCQ Practice
1. Which container gives average O(1) key-based lookup?
unordered_map uses hashing for average O(1) lookup, while map uses a balanced tree with O(log n).
2. Which container provides O(1) random access by index?
std::vector stores elements contiguously, allowing constant-time indexed access; list and the associative containers do not.
3. What guarantee does std::set provide about its elements?
std::set stores unique keys kept in sorted order using a balanced binary search tree.
Flash Cards
Three STL container categories? — Sequence (vector, deque, list), associative (map, set), and unordered/hash-based (unordered_map, unordered_set).
map vs unordered_map complexity? — map is O(log n) sorted via a tree; unordered_map is average O(1) via hashing but unordered.
vector random access complexity? — O(1), because elements are stored contiguously in memory.
What does std::set guarantee? — Unique keys kept in sorted order; inserting a duplicate has no effect.