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.
member object
Container c |
underlying container |
non-member function
Compares the values in the stack lexicographically
operator==,!=,<,<=,>,>=(std::stack)
template< class T, class Container > bool operator==( const stack<T,Container>& lhs, const stack<T,Container>& rhs ); |
(1) | |
template< class T, class Container > bool operator!=( const stack<T,Container>& lhs, const stack<T,Container>& rhs ); |
(2) | |
template< class T, class Container > bool operator<( const stack<T,Container>& lhs, const stack<T,Container>& rhs ); |
(3) | |
template< class T, class Container > bool operator<=( const stack<T,Container>& lhs, const stack<T,Container>& rhs ); |
(4) | |
template< class T, class Container > bool operator>( const stack<T,Container>& lhs, const stack<T,Container>& rhs ); |
(5) | |
template< class T, class Container > bool operator>=( const stack<T,Container>& lhs, const stack<T,Container>& rhs ); |
(6) |
Compares the underlying containers of two container adapters. Compare by applying the corresponding operator to the underlying container.
parameter
lhs, rhs | - | iterator adapter to compare content |
- T Must meet the requirements of EqualityComparable . |
return value
true if the corresponding comparison yields true, otherwise false.
the complexity
linear in container size
specialization of the std::swap algorithm
std::swap(std::stack)
template< class T, class Container > void swap( stack<T,Container>& lhs, stack<T,Container>& rhs ); |
(before C++17) | |
template< class T, class Container > void swap( stack<T,Container>& lhs, stack<T,Container>& rhs ) noexcept(/* see below */); |
(since C++17) |
Specializes the std::swap algorithm for std::stack. lhs
Swaps rhs
the contents of and . Call lhs. swap(rhs).
This overload only participates in overload resolution if std::is_swappable<Container>::value is true. |
(since C++17) |
parameter
lhs, rhs | - | container to swap content |
return value
(none)
the complexity
Same as swapping the underlying container.
abnormal
noexcept specifies: noexcept(noexcept(lhs.swap(rhs))) |
(since C++17) |
Auxiliary class
specialize the std::uses_allocator type trait
std::uses_allocator<std::stack>
template< class T, class Container, class Alloc > struct uses_allocator<stack<T,Container>,Alloc> : std::uses_allocator<Container, Alloc>::type { }; |
(since C++11) |
为 std::stack 提供 std::uses_allocator 类型特性的通透特化:容器适配器使用分配器,若且唯若底层容器使用。
继承自 std::integral_constant
成员常量
value [静态] |
true (公开静态成员常量) |
成员函数
operator bool |
转换对象为 bool ,返回 value (公开成员函数) |
operator() (C++14) |
返回 value (公开成员函数) |
成员类型
类型 | 定义 |
value_type |
bool |
type |
std::integral_constant<bool, value> |
调用示例
#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, const std::stack<Cell> &stack)
{
std::cout << name ;
std::stack<Cell> stackp = stack;
while (stackp.size() > 0)
{
std::cout << stackp.top() << " ";
stackp.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;
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;
//2) 以 cont 的内容复制构造底层容器 c 。
std::stack<Cell> stack1(deque1);
std::stack<Cell> stack2(deque1);
stackPrint("stack1: ", stack1);
stackPrint("stack2: ", stack2);
std::stack<Cell> stack3(deque2);
stackPrint("stack3: ", stack3);
std::cout << std::endl;
//比较二个容器适配器的底层容器。通过应用对应的运算符到底层容器进行比较。
std::cout << "stack1 == stack2: " << (stack1 == stack2) << std::endl;
std::cout << "stack1 == stack3: " << (stack1 == stack3) << std::endl;
std::cout << "stack1 != stack2: " << (stack1 != stack2) << std::endl;
std::cout << "stack1 != stack3: " << (stack1 != stack3) << std::endl;
std::cout << "stack1 < stack2: " << (stack1 < stack2) << std::endl;
std::cout << "stack1 < stack3: " << (stack1 < stack3) << std::endl;
std::cout << "stack1 <= stack2: " << (stack1 <= stack2) << std::endl;
std::cout << "stack1 <= stack3: " << (stack1 <= stack3) << std::endl;
std::cout << "stack1 > stack2: " << (stack1 > stack2) << std::endl;
std::cout << "stack1 > stack3: " << (stack1 > stack3) << std::endl;
std::cout << "stack1 >= stack2: " << (stack1 >= stack2) << std::endl;
std::cout << "stack1 >= stack3: " << (stack1 >= stack3) << std::endl;
//为 std::stack 特化 std::swap 算法。交换 lhs 与 rhs 的内容。
std::swap(stack1, stack3);
stackPrint("stack1: ", stack1);
stackPrint("stack3: ", stack3);
return 0;
}
输出