Internal Data Structures and Time Complexity Table of All the C++ STL Containers
Last Updated :
16 Dec, 2022
As we all are using STL quite heavily in our programs in Interviews, Coding Contests. So knowing the Time complexity of the most frequently used operation and the data structure used behind the scene (in the internal implementation of the STL Library) is a must to know information.
C++ STL Containers with Time Complexity & Data Structure used Behind the Scene:
S. No.
STL Container Name
Internal Data-Structure
Time Complexity
1.
Unordered Set
Hash Table (A hash table uses a hash function to compute an index)
For Insert, Search, Delete: Best: O(1) Avg: O(1) Worst: O(N)
2.
Set
Red Black Tree (self-balancing binary search tree – maintains the height of tree log N)
For Insert, Search, Delete: Best: O(log N) Avg: O(log N) Worst: O(log N)
3.
MultiSet
[Set that stores duplicate elements also]
Threaded Red Black Tree (self-balancing binary search tree – maintains the height of tree log N)
For Insert, Search, Delete: Best: O(log N) Avg: O(log N) Worst: O(log N)
4.
Unordered Map
Hash Table (A hash table uses a hash function to compute an index)
For Insert, Search, Delete: Best: O(1) Avg: O(1) Worst: O(N)
5.
Map
Red Black Tree (self-balancing binary search tree – maintains the height of tree log N)
For Insert, Search, Delete: Best: O(log N) Avg: O(log N) Worst: O(log N)
6.
MultiMap
[Map that stores duplicate keys also]
Threaded Red Black Tree (self-balancing binary search tree – maintains the height of tree log N)
For Insert, Search, Delete: Best: O(log N) Avg: O(log N) Worst: O(log N)
5.
Priority_queue
Max Heap
Insert/Push: O(log N) Delete/Pop: O(log N) Peek/Top: O(1)
6.
Stack
Linked List
Push: O(1) Pop: O(1) Top: O(1)
7.
Queue
Linked List
Push: O(1) Pop: O(1) Front: O(1) Back: O(1)
This is a kind of look-up table to know the Time Complexity of each operation , the short reason can be quickly interpreted from the Data Structure used to internally implement the container in C++ STL.
Also from the Time Complexities of each operation, we can easily calculate the Time Complexity of the entire program.
Share your thoughts in the comments
Add Your Comment
Please Login to comment...