STL containers

Jimmy (xiaoke) Shen
2 min readMar 19, 2021

--

I found I got confused by using the STL API sometimes. For example, should I use push_back for stack or push? Should I use top to access the last element or back? In order to be clear about this, I am searching some online tutorials. The first two charts are pretty helpful. The third one rocks. As it contains a comprehensive comparsion.

A quick comparison of the STL can be found from the following two images. Cheat sheet?

STL container comparison

From [1]
From [2]

STL container library overview

We should know:

  • deque is a basic container.
  • stack and queue are adapted from deque by default
  • priority_queue is adapted from vector by default
From [3]

A clear one can be found here.

Stack is abstracted in the following way, this is why we are using top instead of back

Image is from [3]

Priority_queue can be think as a tree structure, this explains why it is also using top.

Reference

[1] https://www.modernescpp.com/index.php/c-core-guidelines-std-array-and-std-vector-are-your-friends

[2] https://www.hackerearth.com/practice/notes/c-stls-when-to-use-which-stl/

[3] https://medium.com/@shawnnkoski/data-structures-stack-f34642489ac6

--

--