[Solved] What is the meaning of std::stack s1;


The type:

std::stack<int,std::vector<int>>

means that we wish to construct a stack of integers, with the container for the stack implemented as a vector of integers. More detail, in the unlikely event you need it, can be found here.

In other words, create a vector of integers, then wrap the stack type around that.

A stack can be built from various underlying data structures, as long as they provide the facilities specified by a SequenceContainer, as well as back(), push_back() and pop_back(). In the standard library, that means one of vector, deque (the default), or list.


Keep in mind that the underlying container in no way affects what you can do with the stack. You do not get direct access to that container.

So, for example, even though a vector allows random access, a stack built around a vector does not.

All you get is the standard stack methods: assignment, peeking at the top item, getting the size, checking if empty, pushing an item (in-place or move/copy), popping an item, and swapping the top two items.

Nowhere in that list can be seen affecting an element that’s not one of the top two, something that would be relatively easy with a vector.

6

solved What is the meaning of std::stack> s1;