c++11 标准模板(STL)(std::stack)(五)
定义于头文件 <stack>
template<
class T, > class stack; |
std::stack
类是容器适配器,它给予程序员栈的功能——特别是 FILO (先进后出)数据结构。
该类模板表现为底层容器的包装器——只提供特定函数集合。栈从被称作栈顶的容器尾部推弹元素。
成员对象
Container c |
底层容器 |
非成员函数
按照字典顺序比较 stack 中的值
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) |
比较二个容器适配器的底层容器。通过应用对应的运算符到底层容器进行比较。
参数
lhs, rhs | - | 要比较内容的迭代器适配器 |
- T 必须满足可相等比较 (EqualityComparable) 的要求。 |
返回值
若对应比较产出 true 则为 true ,否则为 false 。
复杂度
与容器大小成线性
特化 std::swap 算法
std::swap(std::stack)
template< class T, class Container >
void swap( stack<T,Container>& lhs, stack<T,Container>& rhs ); |
(C++17 前) | |
template< class T, class Container >
void swap( stack<T,Container>& lhs, stack<T,Container>& rhs ) noexcept(/* see below */); |
(C++17 起) |
为 std::stack 特化 std::swap 算法。交换 lhs
与 rhs
的内容。调用 lhs.swap(rhs) 。
此重载仅若 std::is_swappable<Container>::value 为 true 才参与重载决议。 |
(C++17 起) |
参数
lhs, rhs | - | 要交换内容的容器 |
返回值
(无)
复杂度
与交换底层容器相同。
异常
noexcept 规定:
noexcept(noexcept(lhs.swap(rhs))) |
(C++17 起) |
辅助类
特化 std::uses_allocator 类型特性
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 { }; |
(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;
}
输出