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.
Construct stack
std::stack<T,Container>::stack
stack() : stack(Container()) { } |
(1) | (since C++11) |
explicit stack( const Container& cont = Container() ); |
(2) | (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 Alloc > |
(6) | (since C++11) |
template< class Alloc > |
(7) | (since C++11) |
template< class Alloc > |
(8) | (since C++11) |
template< class Alloc > |
(9) | (since C++11) |
template< class Alloc > |
(10) | (since C++11) |
Constructs a new underlying container for container adapters from various data sources.
1) Default constructor. Value-initialized container.
2) Constructs the underlying container with a copy of the contents cont
of c
. This is also the default constructor. (until C++11)
3) Move constructs the underlying container with std::move(cont) c
.
4) Copy constructor. The adapter is constructed by copying the contents of other.c. (implicit declaration)
5) Move constructor. The adapter is constructed with std::move(other.c). (implicit declaration)
6-10) The following constructors are defined only if std::uses_allocator<container_type, Alloc>::value == true, ie the underlying container is a container with allocators (true for all standard library containers).
6) Construct the underlying container with alloc
as the allocator, just like c(alloc).
7) Constructs the underlying container with the contents cont
of and as the allocator, as with c(cont, alloc).alloc
8) Use move semantics cont
with the contents of and alloc
construct the underlying container with as the allocator, just like c(std::move(cont), alloc) .
9) Constructs an adapter with the contents other.c
of and with alloc
as the allocator, as with c(other.c, alloc).
10) Uses move semantics other
with the content of and alloc
constructs an adapter with as an allocator, as with c(std::move(other.c), alloc) .
parameter
alloc | - | The allocator used for all memory allocations by the underlying container |
other | - | Another container adapter used to source initialize the underlying container |
cont | - | Container to use as source to initialize the underlying container |
first, last | - | element to initialize |
type requirements | ||
- Alloc Must meet the requirements of the Allocator . |
||
- Container Must meet the container (Container) requirements. Define the constructor only Container if the requirements of the AllocatorAwareContainer are met (5-10) |
||
-Must InputIt meet the requirements of LegacyInputIterator . |
the complexity
Same as the corresponding operation on the packaged container.
Destruct the stack
std::stack<T,Container>::~stack
~stack(); |
Destroys the container adapter. The element's destructor is called, then the used storage is deallocated. Note that if the element is a pointer, the pointed object is not destroyed.
the complexity
Linear with container adapter size.
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;
};
//1) 默认构造函数。值初始化容器。
std::stack<Cell> stack1;
std::cout << "stack1 empty: " << stack1.empty() << std::endl;
std::cout << std::endl;
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> stack2(deque1);
stackPrint("stack2: ", stack2);
std::cout << std::endl;
//3) 以 std::move(cont) 移动构造底层容器 c 。
std::stack<Cell> stack3(std::move(deque1));
stackPrint("stack3: ", stack3);
std::cout << std::endl;
std::deque<Cell> deque2(6);
std::generate(deque2.begin(), deque2.end(), generate);
std::cout << "deque2: ";
std::copy(deque2.begin(), deque2.end(), std::ostream_iterator<Cell>(std::cout, " "));
std::cout << std::endl;
std::stack<Cell> stack4(deque2);
//4) 复制构造函数。适配器以 other.c 的内容复制构造。
std::stack<Cell> stack5(stack4);
stackPrint("stack5: ", stack5);
std::cout << std::endl;
//5) 移动构造函数。适配器以 std::move(other.c) 构造。
std::stack<Cell> stack6(std::move(stack4));
stackPrint("stack6: ", stack6);
std::cout << "stack4 empty: " << stack4.empty() << std::endl;
return 0;
}
output