0
stack<int,vector<int>>s1
stack<int,list<int>>s1
stack<int>s1

Is there any advantage of using vector<int> over the default stack version?

user788888
  • 27
  • 3
  • _"What is the use of initializing a “stack” variable in c++ with different parameters?"_ You could use your own user defined container type. –  May 03 '21 at 08:03
  • 1
    A `stack` is basically just a wrapper around one of these three containers. Imagine if `stack` didn't exist and you had to use one of the three directly to emulate it. Which one would you pick? (depending on how you use it. Vector is more space-efficient, but it's also awkward to resize, and so on) – IWonderWhatThisAPIDoes May 03 '21 at 08:04
  • 1
    Changing allocators and default containers are mainly relevant for things like embedded systems for which resources are limited, or where you need more control over the resources. `std::vector` is guaranteed to have continuous memory, while `std::deque` can consists of different chunks, and while the requirements of `continuous memory` can have downsides for the performance of a `std::stack` it could also be beneficial in resource-limited environments. – t.niese May 03 '21 at 08:59

1 Answers1

0

They are different constructors for setting up a stack STL.

stack<int> s1 Creates an empty stack ( with no elements inside ) , You need to push the int values into the stack manually after creating this.

stack<int,vector<int>>s1 stack<int,list<int>>s1 , Both of them initialises a stack , however you can directly assign the values of the stack by passing an already existing vector or list into that as

std::vector<int> a (2,20);   // Creates a vector
std::stack<int,std::vector<int> > s1 (a);   // initialises a stack with a vector

You can see the further page for more references http://www.cplusplus.com/reference/stack/stack/stack/

ThivinAnandh
  • 286
  • 1
  • 8
  • 1
    While this is true, it might be a bit misleading. Using `std::stack>` instead of `std::stack` changes the container that is used by stack from `std::deque` to `std::vector` which can result in differences in performance. If a `stack` based on `std::vector` grows there can be more copy or move constructions be involved then for one based on `std::deque`. – t.niese May 03 '21 at 08:44