STL Containers in C++
π₯ STL Containers Classification:
Section titled βπ₯ STL Containers Classification:β- Sequence Containers β Store data sequentially.
vectordequelistarray
- Associative Containers β Store key-value pairs with automatic sorting.
mapsetmultimapmultiset
- Unordered Associative Containers β Store key-value pairs without sorting.
unordered_mapunordered_setunordered_multimapunordered_multiset
- Container Adapters β Provide modified interface over other containers.
queuestackpriority_queue
Containers that Do Not Support Random Access: β
Section titled βContainers that Do Not Support Random Access: ββ#include <map>:std::map,std::unordered_map- Access by keymap[key]#include <set>:std::set,std::unordered_set- Access via iterator or range-based loopfor (const auto& elem : set)#include <list>:std::list,std::forward_list- Access via iterator or range-based loopfor (const auto& elem : list)#include <queue>:std::queue- Access byqueue.front(),queue.back()#include <stack>:std::stack- Access bystack.top()#include <deque>:std::deque- Partially supports indexing but slower thanstd::vectordeque[index]#include <utility>:std::pair- Access bypair.first,pair.second
Container Functions vs std:: Algorithm Functions in C++ β
Section titled βContainer Functions vs std:: Algorithm Functions in C++ ββfind() -> Vector & map
// Sequential Container (use std::find)std::vector<int> v = {1, 2, 3, 4, 5};auto it1 = std::find(v.begin(), v.end(), 3); // O(N)// Associative Container (use container.find)std::map<int, std::string> m = {{1, "One"}, {2, "Two"}, {3, "Three"}};auto it2 = m.find(2); // O(log N)sort() -> Vector and List
// For vector β use std::sortstd::vector<int> v = {5, 3, 1, 4, 2};std::sort(v.begin(), v.end()); // O(N log N)// For list β use container.sortstd::list<int> l = {5, 3, 1, 4, 2};l.sort(); // O(N log N) using Merge Sortcount -> Vector and Set
// For vector β use std::countstd::vector<int> v = {1, 2, 3, 1, 2, 1};int count1 = std::count(v.begin(), v.end(), 1); // O(N)// For set β use container.countstd::set<int> s = {1, 2, 3, 4, 5};int count2 = s.count(3); // O(log N)lower_boound() -> Vector & Map
// Vector β std::lower_boundstd::vector<int> v = {1, 3, 5, 7, 9};auto it1 = std::lower_bound(v.begin(), v.end(), 5); // O(log N)// Map β container.lower_boundstd::map<int, std::string> m = {{1, "One"}, {3, "Three"}, {5, "Five"}};auto it2 = m.lower_bound(3); // O(log N)β Best Practices:
- Use container member functions (
.find(),.count()) for associative containers β faster. - Use
std::algorithm functions for sequential containers β general-purpose algorithms. - Prefer
std::sort()for sorting unless usinglist, which has its own.sort(). - Use
std::binary_search()instead ofstd::find()for sorted vectors β faster lookup.
.back() & .front()
Section titled β.back() & .front()βSupports .back() and .front() β
queuedequelistvector
Does NOT support .back() and .front() β
stackβ Only.top()is available.set/unordered_setβ No.back()or.front()(since sets are not indexed).
push_back vs push (or pop_back vs pop)
Section titled βpush_back vs push (or pop_back vs pop)βpush_back + pop_back()
- Containers -
vector,deque,list, `string - Adds an element at the end or Removes the last element
- Preserves order
- TC :
O(1)
push() + pop()
- Containers -
stack,queue,priority_queue - Adds an element at the top (stack) or back (queue) or Removes the top (stack) or front (queue) element
- Follows LIFO (stack) or FIFO (queue) behavior
- TC :
O(1)
C++ Data Structures and Best Containers for Operations
Section titled βC++ Data Structures and Best Containers for Operationsβ1. Lookup / Find
- Purpose: Retrieve the value or check if an element exists.
- Best Containers: β
std::unordered_map/std::unordered_setβ O(1) average time, O(N) worst case.std::map/std::setβ O(log N).std::vector/std::deque/std::listβ O(N) (Linear search).
- Note: Use unordered_map/set for fast lookup. Use map/set if you need sorted keys. β
2. Insertion
- Purpose: Add new elements to the container.
- Best Containers:
std::unordered_map/std::unordered_setβ O(1) average, O(N) worst case.std::map/std::setβ O(log N).std::vectorβ- End: O(1) amortized.
- Middle: O(N).
std::dequeβ O(1) at both ends, O(N) in the middle.std::listβ O(1) insertion at any point (using iterator).
- Note: Use unordered_map/set for fast average-time insertion. Use list for efficient insertion at any position.
3. Deletion / Erasure
- Purpose: Remove elements from the container.
- Best Containers:
std::unordered_map/std::unordered_setβ O(1) average, O(N) worst case.std::map/std::setβ O(log N).std::vector/std::dequeβ- End: O(1).
- Middle: O(N) due to shifting.
std::listβ O(1) deletion (using iterator).
- Note: Use unordered_map/set for fast deletion. Use list for efficient removal at any position.
4. Count (Frequency Counting)
- Purpose: Count occurrences of elements.
- Best Containers:
std::unordered_map/std::mapβ Store element as the key and count as the value.std::unordered_multiset/std::multisetβ Auto-maintains element counts.
- Note: Use unordered_map for faster frequency counting. Use multiset for built-in count management.
5. Access (Indexing / Random Access)
- Purpose: Access elements by position.
- Best Containers:
std::vector/std::deque/int arr[]β O(1) direct access.std::list/std::forward_listβ O(N) (No direct access, requires traversal).std::map/std::setβ O(log N).
- Note: Use vector for fast random access. Use map for key-based access.
6. Iteration (Traversal)
- Purpose: Iterate over all elements.
- Best Containers:
std::vector/std::deque/std::listβ O(N) sequential iteration.std::map/std::set/std::multimap/std::multisetβ O(N) in-order traversal.std::unordered_map/std::unordered_setβ O(N) unordered iteration.
- Note: Use vector for simple sequential iteration. Use map for ordered iteration.
7. Sorting
- Purpose: Arrange elements in ascending/descending order.
- Best Containers:
std::map/std::setβ Always sorted by default (O(log N) insertion maintains order).std::vectorβ O(N log N) withstd::sort().std::dequeβ O(N log N) withstd::sort().std::listβ O(N log N) withstd::list::sort().
- Note: Use map/set for automatic sorting. Use vector +
std::sort()for custom sorting.