(C++17) | ||||
Sequence | ||||
(C++11) | ||||
(C++26) | ||||
(C++26) | ||||
(C++11) | ||||
Associative | ||||
Unordered associative | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
(C++11) | ||||
Adaptors | ||||
stack | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
(C++23) | ||||
Views | ||||
(C++20) | ||||
(C++23) | ||||
Tables | ||||
Iterator invalidation | ||||
Member function table | ||||
Non-member function table |
std::stack
Member functions | ||||
Element access | ||||
Capacity | ||||
Modifiers | ||||
(C++23) | ||||
(C++11) | ||||
(C++11) | ||||
Non-member functions | ||||
(C++11) | ||||
Helper classes | ||||
(C++11) | ||||
(C++23) | ||||
Deduction guides(C++17) |
Defined in header <stack> | ||
template< class T, | ||
Thestd::stack
class is acontainer adaptor that gives the programmer the functionality of astack - specifically, a LIFO (last-in, first-out) data structure.
The class template acts as a wrapper to the underlying container - only a specific set of functions is provided. The stack pushes and pops the element from the back of the underlying container, known as the top of the stack.
All member functions ofstd::stack areconstexpr: it is possible to create and usestd::stack objects in the evaluation of a constant expression.However, | (since C++26) |
Contents |
T | - | The type of the stored elements. The program is ill-formed ifT is not the same type asContainer::value_type . |
Container | - | The type of the underlying container to use to store the elements. The container must satisfy the requirements ofSequenceContainer. Additionally, it must provide the following functions with theusual semantics:
The standard containersstd::vector (includingstd::vector<bool>),std::deque andstd::list satisfy these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard containerstd::deque is used. |
Type | Definition |
container_type | Container [edit] |
value_type | Container::value_type [edit] |
size_type | Container::size_type[edit] |
reference | Container::reference [edit] |
const_reference | Container::const_reference [edit] |
Member | Description |
Container c | the underlying container (protected member object)[edit] |
constructs thestack (public member function)[edit] | |
destructs thestack (public member function)[edit] | |
assigns values to the container adaptor (public member function)[edit] | |
Element access | |
accesses the top element (public member function)[edit] | |
Capacity | |
checks whether the container adaptor is empty (public member function)[edit] | |
returns the number of elements (public member function)[edit] | |
Modifiers | |
inserts element at the top (public member function)[edit] | |
(C++23) | inserts a range of elements at the top (public member function)[edit] |
(C++11) | constructs element in-place at the top (public member function)[edit] |
removes the top element (public member function)[edit] | |
(C++11) | swaps the contents (public member function)[edit] |
lexicographically compares the values of twostack s(function template)[edit] | |
(C++11) | specializes thestd::swap algorithm (function template)[edit] |
specializes thestd::uses_allocator type trait (class template specialization)[edit] | |
(C++23) | formatting support forstd::stack (class template specialization)[edit] |
Deduction guides | (since C++17) |
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_containers_ranges | 202202L | (C++23) | Ranges construction and insertion for containers |
__cpp_lib_constexpr_stack | 202502L | (C++26) | constexprstd::stack |
This section is incomplete Reason: no example |
The following behavior-changing defect reports were applied retroactively to previously published C++ standards.
DR | Applied to | Behavior as published | Correct behavior |
---|---|---|---|
LWG 307 | C++98 | Container could not bestd::vector<bool> | allowed |
LWG 2566 | C++98 | Missing the requirement forContainer::value_type | ill-formed ifT is not the same type asContainer::value_type |
resizable contiguous array (class template)[edit] | |
space-efficient dynamic bitset (class template specialization)[edit] | |
double-ended queue (class template)[edit] | |
doubly-linked list (class template)[edit] |