Which container is recommended as the default sequence container in C++?
Contiguous storage makes vector cache-friendly and fast for most workloads.
STL Containers · Exercise 1 of 4
Twenty multiple-choice questions on C++ STL containers: vector, deque, list, set, map, unordered_map, adapters, complexities and iterator invalidation. Pick an option to check your answer instantly.
Which container is recommended as the default sequence container in C++?
Contiguous storage makes vector cache-friendly and fast for most workloads.
How is std::map typically implemented?
Hence keys are kept sorted and operations are O(log n).
What is the average time complexity of find() in std::unordered_map?
The worst case is O(n), when many keys collide in one bucket.
What happens when you insert a value that is already in a std::set?
Use std::multiset to keep duplicates.
Which operation is O(1) for std::deque but O(n) for std::vector?
deque grows at both ends; vector must shift every element to insert at the front.
Why does std::list not provide operator[]?
Its iterators are bidirectional, not random-access.
m is a std::map<std::string, int> without the key "x". What does int v = m["x"]; do?
at() throws instead; find() and contains() look up without inserting.
With the default comparator, what does std::priority_queue<int>::top() return?
The default std::less gives a max-heap; std::greater gives a min-heap.
Which container does std::stack use underneath by default?
You can choose another: std::stack<int, std::vector<int>>.
Which operation can invalidate all iterators to a vector's elements?
Reallocation moves the elements to new memory.
Timed online tests with instant scores and explanations.