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.
element access
Access the top element of the stack
std::stack<T,Container>::top
reference top(); |
|
const_reference top() const; |
Returns a reference to the top element in the stack. It is the most recently pushed element. This element will be removed when pop() is called. Equivalent to calling c.back().
parameter
(none)
return value
reference to the end element
the complexity
constant
modifier
Insert an element to the top of the stack
std::stack<T,Container>::push
void push( const value_type& value ); |
||
void push( value_type&& value ); |
(since C++11) |
Push the given element value
to the top of the stack.
1) Equivalently call c.push_back(value)
2) Equivalently call c.push_back(std::move(value))
parameter
value | - | the element value to push |
return value
(none)
the complexity
Equal to the complexity of Container::push_back.
In situ construction of elements on top
std::stack<T,Container>::emplace
template< class... Args > |
(since C++11) (until C++17) |
|
template< class... Args > |
(since C++17) |
Pushes a new element to the top of the stack. Constructs the element in-place, that is, no move or copy operations are performed. Calls the element's constructor with exactly the same arguments that were supplied to the function.
Equivalently calls c.emplace_back(std::forward<Args>(args)...); .
parameter
args | - | Parameters forwarded to the element's constructor |
return value
(none) | (before C++17) |
The value or reference returned by the above call to Container::emplace_back, if it exists. | (since C++17) |
the complexity
Equivalent to the complexity of Container::emplace_back.
remove the top element of the stack
std::stack<T,Container>::pop
void pop(); |
Removes the top element from the stack. Equivalently calls c.pop_back().
parameter
(none)
return value
(none)
the complexity
Equal to the complexity of Container::pop_back.
exchange content
std::stack<T,Container>::swap
void swap( stack& other ) noexcept(/* see below */); |
(since C++11) |
Swaps the content other
of . Equivalently calls using std::swap; swap(c, other.c); .
parameter
other | - | The container adapter to swap content with |
return value
(none)
example
noexcept specifies: noexcept(noexcept(swap(c, other.c))) In the above expression, the identifier is looked up in the same way as used by the C++17 std::is_nothrow_swappable attribute |
(before C++17) |
noexcept specifies: noexcept(std::is_nothrow_swappable<Container>::value) |
(since C++17) |
the complexity
Same as the underlying container (typically a constant).
call example
#include <iostream>
#include <forward_list>
#include <string>
#include <iterator>
#include <algorithm>
#include <functional>
#include <stack>
#include <deque>
#include <time.h>
using namespace std;
struct Cell
{
int x;
int y;
Cell() = default;
Cell(int a, int b): x(a), y(b) {}
Cell &operator +=(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator +(const Cell &cell)
{
x += cell.x;
y += cell.y;
return *this;
}
Cell &operator *(const Cell &cell)
{
x *= cell.x;
y *= cell.y;
return *this;
}
Cell &operator ++()
{
x += 1;
y += 1;
return *this;
}
bool operator <(const Cell &cell) const
{
if (x == cell.x)
{
return y < cell.y;
}
else
{
return x < cell.x;
}
}
bool operator >(const Cell &cell) const
{
if (x == cell.x)
{
return y > cell.y;
}
else
{
return x > cell.x;
}
}
bool operator ==(const Cell &cell) const
{
return x == cell.x && y == cell.y;
}
};
std::ostream &operator<<(std::ostream &os, const Cell &cell)
{
os << "{" << cell.x << "," << cell.y << "}";
return os;
}
void stackPrint(const std::string &name, std::stack<Cell> &stack)
{
std::cout << name ;
while (stack.size() > 0)
{
std::cout << stack.top() << " ";
stack.pop();
}
std::cout << std::endl;
}
int main()
{
std::cout << std::boolalpha;
std::mt19937 g{std::random_device{}()};
srand((unsigned)time(NULL));
auto generate = []()
{
int n = std::rand() % 10 + 110;
Cell cell{n, n};
return cell;
};
std::deque<Cell> deque1(6);
std::generate(deque1.begin(), deque1.end(), generate);
std::cout << "deque1: ";
std::copy(deque1.begin(), deque1.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
//2) 以 cont 的内容复制构造底层容器 c 。
std::stack<Cell> stack1(deque1);
while (!stack1.empty())
{
//返回 stack 中顶元素的引用。它是最近推入的元素。
//此元素将在调用 pop() 时被移除。等效于调用 c.back() 。
//const_reference
std::cout << "stack1.top() before: " << stack1.top() << " ";
//reference
stack1.top() = generate();
std::cout << "after: " << stack1.top();
//从 stack 移除顶元素。等效地调用 c.pop_back()
stack1.pop();
std::cout << std::endl;
}
std::cout << std::endl;
std::stack<Cell> stack2;
while (stack2.size() < 6)
{
Cell cell = generate();
if (stack2.size() % 2 == 0)
{
//推给定的元素 value 到 stack 顶。
//1) 等效地调用 c.push_back(value)
stack2.push(cell);
}
else
{
//推给定的元素 value 到 stack 顶。
//2) 等效地调用 c.push_back(std::move(value)),移动语义
stack2.push(std::move(cell));
}
std::cout << "stack2.top() : " << stack2.top() ;
std::cout << std::endl;
}
std::cout << std::endl;
std::stack<Cell> stack3;
while (stack3.size() < 6)
{
int n = std::rand() % 10 + 110;
//推入新元素到 stack 顶。原位构造元素,即不进行移动或复制操作。
//以与提供给函数者准确相同的参数调用元素的构造函数。
//等效地调用 c.emplace_back(std::forward<Args>(args)...);
stack3.emplace(n, n);
std::cout << "stack3.top() : " << stack3.top() ;
std::cout << std::endl;
}
std::cout << std::endl;
//交换容器适配器与 other 的内容。
//等效地调用 using std::swap; swap(c, other.c);
stack2.swap(stack3);
stackPrint("stack2: ", stack2);
stackPrint("stack3: ", stack3);
return 0;
}
output