Skip to content
Related Articles

Related Articles

Internal Data Structures and Time Complexity Table of All the C++ STL Containers

Improve Article
Save Article
  • Difficulty Level : Expert
  • Last Updated : 20 Jul, 2022
Improve Article
Save Article

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.

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)

4.

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)

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.

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!