Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Stack implemented in STL?

I came across this:

Standard C++ Containers

It triggered me the question, how are stacks implemented in STL?

I am looking for a description similar to:

How is C++ std::vector implemented?

What really is a deque in STL?

like image 335
ssk Avatar asked Sep 20 '25 23:09

ssk


1 Answers

stack is an adapter which uses another container for the underlying storage, and links the functions push, pop, emplace etc. to the relevant functions in the underlying container.

By default, std::stack uses std::deque as underlying container. But you can specify your own, e.g. std::stack<T, std::vector<T>> s;.

For more details about this, see cppreference.

like image 65
M.M Avatar answered Sep 22 '25 13:09

M.M