Defined in the header file <stack>
template< class T, |
std::stack
Classes are container adapters that give the programmer the functionality of a stack -- specifically a FILO (first in, last out) data structure.
The class template behaves as a wrapper around the underlying container - providing only a specific set of functions. The stack pushes and pops elements from the end of the container called the top of the stack.
template parameter
T | - | The stored element type. The behavior is undefined if T and Container::value_type is not the same type. (since C++17) |
Container | - | The underlying container type used to store elements. A container must meet the requirements of a sequence container (SequenceContainer). Additionally, it must provide the following functions with the usual semantics:
The standard containers std::vector, std::deque, and std::list satisfy these requirements. If no container class is specified for a particular |
member type
member type | definition |
container_type |
Container |
value_type |
Container::value_type |
size_type |
Container::size_type |
reference |
Container::reference |
const_reference |
Container::const_reference |
member function
(Constructor) |
constructor stack (public member function) |
(destructor) |
Destructor stack (public member function) |
operator= |
Assign to container adapter (public member function) |
element access |
|
top |
Access the top element of the stack (public member function) |
capacity |
|
empty |
Check if the underlying container is empty (public member function) |
size |
Returns the number of elements held (public member function) |
modifier |
|
push |
Insert an element to the top of the stack (public member function) |
place (C++11) |
Constructs element in-place at the top (public member function) |
pop |
Delete the top element of the stack (public member function) |
swap |
exchange content (public member function) |
member object |
|
Container c |
underlying container (protected member object) |
non-member function
operator==operator!=operator<operator<=operator>operator>= |
Compares the values in the stack lexicographically (function template) |
std::swap(std::stack) |
specialization of the std::swap algorithm (function template) |
Auxiliary class
std::uses_allocator<std::stack>(C++11) |
specialize the std::uses_allocator type trait (function template) |
std::stack
derivation guide
template<class Container> stack(Container) -> stack<typename Container::value_type, Container>; |
(1) | (since C++17) |
template<class Container, class Allocator> stack(Container, Allocator) -> stack<typename Container::value_type, Container>; |
(2) | (since C++17) |
为 stack 提供推导指引以允许从底层容器类型推导。此重载仅若 Alloc
满足分配器 (Allocator) ,且 Container
不满足分配器 (Allocator) ,而对于重载 (2) ,若 std::uses_allocator_v<Container, Allocator> 为 true
才参与重载决议
注意:库确定类型是否满足遗留输入迭代器 (LegacyInputIterator) 的程度是未指定的,除了最低要求是整数类型不具备输入迭代器的条件。类似地,确定类型是否满足分配器 (Allocator) 是未指定的,除了最低要求是成员类型 Alloc::value_type
必须存在,且表达式 std::declval<Alloc&>().allocate(std::size_t{}) 在作为不求值运算数时必须为良式。