New
Explore Problems Interview Contest Discuss Store Premium Sign up or Sign in
Back C++ STL Guide | STL Operations and Time Complexities
user5648r 47 Last Edit: July 12, 2021 2:38 PM 873 VIEWS
26
STL Containers
Containers library
1. Sequence containers
Sequence containers implement data structures which can be accessed sequentially.
1. array : (C++11) static contiguous array
2. vector : dynamic contiguous array
3. deque : double-ended queue
4. forward_list (C++11) : singly-linked list
5. list : doubly-linked list
2. Associative containers
Associative containers implement sorted data structures that can be quickly searched (O(log n) complexity).
1. set : collection of unique keys, sorted by keys
2. map : collection of key-value pairs, sorted by keys, keys are unique
3. multiset : collection of keys, sorted by keys
4. multimap : collection of key-value pairs, sorted by keys
3. Unordered associative containers
Unordered associative containers implement unsorted (hashed) data structures that can be quickly searched (O(1) amortized, O(n) worst-case
complexity).
1. unordered_set : collection of unique keys, hashed by keys
2. unordered_map : collection of key-value pairs, hashed by keys, keys are unique
3. unordered_multiset : collection of keys, hashed by keys
4. unordered_multimap : collection of key-value pairs, hashed by keys
4. Container adaptors
Container adaptors provide a different interface for sequential containers.
1. stack
2. queue
3. priority_queue
Now, lets look for the time complexities of containers
1. Priority Queue
2. Map : Time Complexities mentioned below are for Ordered Map.
3. Set : Time Complexities mentioned below are for Ordered Set.
4. Stack
5. Queue
6. Vector
7. List
Sr. Data Time Space
Sub Type Syntax Operations Comme
No Structure Complexity Complexity
Priority priority_queue<data_type>
1 Max Heap Q.top() O(1) O(1)
Queue Q
priority_queue<data_type,
Min Heap vector<data_type>, Q.push() O(log n) O(1)
greater<data_type>> Q
Q.pop() O(log n) O(1)
Q.empty() O(1) O(1)
The map <int, int> M i
Ordered
2 Map map <int, int> M M.find(x) O(log n) O(1) implementation of
Map
Black Trees.
The unordered_map<
Unordered unordered_map<int, int> M.insert(pair<int, implementation of Has