public member function
<stack>

std::stack::stack

explicit stack (const container_type& ctnr = container_type());
initialize (1)
explicit stack (const container_type& ctnr);
move-initialize (2)
explicit stack (container_type&& ctnr = container_type());
allocator (3)
template <class Alloc> explicit stack (const Alloc& alloc);
init + allocator (4)
template <class Alloc> stack (const container_type& ctnr, const Alloc& alloc);
move-init + allocator (5)
template <class Alloc> stack (container_type&& ctnr, const Alloc& alloc);
copy + allocator (6)
template <class Alloc> stack (const stack& x, const Alloc& alloc);
move + allocator (7)
template <class Alloc> stack (stack&& x, const Alloc& alloc);
Construct stack
Constructs a stack container adaptor object.

A container adaptor keeps internally a container object as data. This container object is a copy of the ctnr argument passed to the constructor, if any, otherwise it is an empty container.
A container adaptor keeps internally a container object as data, which is initialized by this constructor:
(1) initialization constructor
Constructs a container adaptor whose internal container is initialized to a copy of ctnr.
(2) move-initialization constructor
Constructs a container adaptor whose internal container acquires the value of ctnr by move-constructing it.
(3) allocator constructor
Constructs a container adaptor whose internal container is constructed with alloc as argument.
(4) initialization with allocator constructor
Constructs a container adaptor whose internal container is constructed with cntr and alloc as arguments.
(5) move-initialization with allocator constructor
Constructs a container adaptor whose internal container is constructed with std::move(cntr) and alloc as arguments.
(6) copy with allocator constructor
Constructs a container adaptor whose internal container is constructed with x's internal container as first argument and alloc as second.
(7) move with allocator constructor
Constructs a container adaptor whose internal container is constructed by moving x's internal container as first argument and passing alloc as second.

Parameters

ctnr
Container object.
container_type is the type of the underlying container type (defined as an alias of the second class template parameter, Container; see member types).
alloc
Allocator object.
Alloc shall be a type for which uses_allocator::value is true (for other types, the constructor does not even participate in overload resolution).
x
A stack of the same type (i.e., with the same template arguments, T and Container).

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// constructing stacks
#include <iostream>       // std::cout
#include <stack>          // std::stack
#include <vector>         // std::vector
#include <deque>          // std::deque

int main ()
{
  std::deque<int> mydeque (3,100);          // deque with 3 elements
  std::vector<int> myvector (2,200);        // vector with 2 elements

  std::stack<int> first;                    // empty stack
  std::stack<int> second (mydeque);         // stack initialized to copy of deque

  std::stack<int,std::vector<int> > third;  // empty stack using vector
  std::stack<int,std::vector<int> > fourth (myvector);

  std::cout << "size of first: " << first.size() << '\n';
  std::cout << "size of second: " << second.size() << '\n';
  std::cout << "size of third: " << third.size() << '\n';
  std::cout << "size of fourth: " << fourth.size() << '\n';

  return 0;
}
Output:
size of first: 0
size of second: 3
size of third: 0
size of fourth: 2


Complexity

One container construction (which for standard containers is up to linear in its size).

Iterator validity

Moving constructors (2) and (7), may invalidate all iterators, pointers and references related to their moved argument.

Data races

All copied elements are accessed.
The moving constructors (2) and (7) may modify their (first) argument.

Exception safety

Provides the same level of guarantees as the operation performed on the container.

See also