Namespaces
Variants
Views
Actions

std::stack<T,Container>::stack

From cppreference.com
< cpp‎ | container‎ | stack
stack() : stack(Container()) { }
(1) (since C++11)
(2)
explicit stack( const Container& cont = Container() );
(until C++11)
explicit stack( const Container& cont );
(since C++11)
explicit stack( Container&& cont );
(3) (since C++11)
stack( const stack& other );
(4)
stack( stack&& other );
(5) (since C++11)
template< class InputIt >
stack( InputIt first, InputIt last );
(6) (since C++23)
template< class Alloc >
explicit stack( const Alloc& alloc );
(7) (since C++11)
template< class Alloc >
stack( const Container& cont, const Alloc& alloc );
(8) (since C++11)
template< class Alloc >
stack( Container&& cont, const Alloc& alloc );
(9) (since C++11)
template< class Alloc >
stack( const stack& other, const Alloc& alloc );
(10) (since C++11)
template< class Alloc >
stack( stack&& other, const Alloc& alloc );
(11) (since C++11)
template< class InputIt, class Alloc >
stack( InputIt first, InputIt last, const Alloc& alloc );
(12) (since C++23)

Constructs new underlying container of the container adaptor from a variety of data sources.

1) Default constructor. Value-initializes the container.
2) Copy-constructs the underlying container c with the contents of cont. This is also the default constructor. (until C++11)
3) Move-constructs the underlying container c with std::move(cont).
4) Copy constructor. The adaptor is copy-constructed with the contents of other.c.
5) Move constructor. The adaptor is constructed with std::move(other.c).
6) Constructs the underlying container c with the contents of the range [first, last). This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator.
7-12) These constructors participate in overload resolution only if std::uses_allocator<Container, Alloc>::value is true, that is, if the underlying container is an allocator-aware container (true for all standard library containers that can be used with stack).
7) Constructs the underlying container using alloc as allocator, as if by c(alloc).
8) Constructs the underlying container with the contents of cont and using alloc as allocator, as if by c(cont, alloc).
9) Constructs the underlying container with the contents of cont using move semantics while utilizing alloc as allocator, as if by c(std::move(cont), alloc).
10) Constructs the adaptor with the contents of other.c and using alloc as allocator, as if by c(other.c, alloc).
11) Constructs the adaptor with the contents of other using move semantics while utilizing alloc as allocator, as if by c(std::move(other.c), alloc).
12) Constructs the underlying container with the contents of the range [first, last) using alloc as allocator, as if by c(first, last, alloc). This overload participates in overload resolution only if InputIt satisfies LegacyInputIterator.

Contents

[edit] Parameters

alloc - allocator to use for all memory allocations of the underlying container
other - another container adaptor to be used as source to initialize the underlying container
cont - container to be used as source to initialize the underlying container
first, last - range of elements to initialize with
Type requirements
-
Alloc must meet the requirements of Allocator.
-
Container must meet the requirements of Container. The constructors taking an allocator parameter participate in overload resolution only if Container meets the requirements of AllocatorAwareContainer.
-
InputIt must meet the requirements of LegacyInputIterator.

[edit] Complexity

Same as the corresponding operation on the wrapped container.

[edit] Notes

Feature testing macro: __cpp_lib_adaptor_iterator_pair_constructor (for overloads (6) and (12))

[edit] Example

#include <stack>
#include <deque>
#include <iostream>
 
int main()
{
    std::stack<int> c1;
    c1.push(5);
    std::cout << c1.size() << '\n';
 
    std::stack<int> c2(c1);
    std::cout << c2.size() << '\n';
 
    std::deque<int> deq {3, 1, 4, 1, 5};
    std::stack<int> c3(deq); // overload (2)
    std::cout << c3.size() << '\n';
 
#   ifdef __cpp_lib_adaptor_iterator_pair_constructor
    const auto il = {2, 7, 1, 8, 2};
    std::stack<int> c4 { il.begin(), il.end() }; // overload (6), C++23
    std::cout << c4.size() << '\n';
#   endif
}

Possible output:

1
1
5
5

[edit] Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
P0935R0 C++11 default constructor was explicit made implicit

[edit] See also

assigns values to the container adaptor
(public member function) [edit]