std::stack
介绍
成员函数
非成员函数
介绍
// stack 模板定义
template<class T, class Container = std::deque<T> > class stack;
- std::stack 类是容器适配器,它给予程序员栈的功能——特别是 FILO(先进后出)数据结构
- 该类模板表现为底层容器的包装器——只提供特定函数集合。栈从被称作栈顶的容器尾部推弹元素
成员函数
构造析构
#include <QCoreApplication>
#include <iostream>
#include <stack>
#include <vector>
auto Print = [](const std::string &msg, const std::stack<int> &sk) {
std::stack<int> s = sk;
std::cout << msg << "\t";
while (!s.empty()) {
std::cout << s.top() << "\t";
s.pop();
}
std::cout << std::endl;
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using st_int = std::stack<int>;
st_int s1; // 默认构造
Print("s1 : ", s1);
s1.push(1);
s1.push(2);
std::stack<int, std::vector<int>> s2; // 指定适配容器构造
Print("s2 : ", s1);
s2.push(3);
s2.push(4);
st_int s3(s1); // 拷贝构造
Print("s3 : ", s3);
std::deque<int> dp;
dp.push_back(5);
dp.push_back(6);
std::allocator<int> alloc;
st_int s4(dp, alloc); // 指定容器,分配器
Print("s4 : ", s4);
st_int s5(s1, alloc); // 拷贝构造,指定分配器
Print("s5 : ", s5);
//析构函数默认
return 0; // a.exec();
}
输出结果:
s1 :
s2 : 2 1
s3 : 2 1
s4 : 6 5
s5 : 2 1
元素访问
#include <QCoreApplication>
#include <iostream>
#include <stack>
#include <vector>
auto Print = [](const std::string &msg, const std::stack<int> &sk) {
std::stack<int> s = sk;
std::cout << msg << "\t";
while (!s.empty()) {
std::cout << s.top() << "\t";
s.pop();
}
std::cout << std::endl;
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using st_int = std::stack<int>;
st_int s1;
s1.push(1);
s1.push(2);
Print("s1 : ", s1);
// 访问栈顶元素
std::cout << "top: " << s1.top() << "\t" << s1.top() << std::endl;
return 0; // a.exec();
}
输出结果:
s1 : 2 1
top: 2 2
容量
#include <QCoreApplication>
#include <iostream>
#include <stack>
#include <vector>
auto Print = [](const std::string &msg, const std::stack<int> &sk) {
std::stack<int> s = sk;
std::cout << msg << "\t";
while (!s.empty()) {
std::cout << s.top() << "\t";
s.pop();
}
std::cout << std::endl;
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using st_int = std::stack<int>;
st_int s1;
s1.push(1);
s1.push(2);
Print("s1 : ", s1);
// 判断容器是否为空
std::cout << "empty : " << std::boolalpha << s1.empty() << std::endl;
// 获取容器数据数量
std::cout << "size : " << s1.size() << std::endl;
return 0; // a.exec();
}
输出结果:
s1 : 2 1
empty : false
size : 2
修改器
#include <QCoreApplication>
#include <iostream>
#include <stack>
#include <vector>
auto Print = [](const std::string &msg, const std::stack<int> &sk) {
std::stack<int> s = sk;
std::cout << msg << "\t";
while (!s.empty()) {
std::cout << s.top() << "\t";
s.pop();
}
std::cout << std::endl;
};
int main(int argc, char *argv[]) {
QCoreApplication a(argc, argv);
using st_int = std::stack<int>;
st_int s1;
s1.push(1); // 压栈
s1.push(2);
s1.emplace(3); // 压栈
s1.emplace(4);
s1.emplace(5);
Print("s1 : ", s1);
s1.pop(); // 弹栈
Print("s1 : ", s1);
s1.pop(); // 弹栈
Print("s1 : ", s1);
st_int s2;
s2.swap(s1); // 容器互换内容
Print("s1 : ", s1);
Print("s2 : ", s2);
return 0; // a.exec();
}
输出结果:
s1 : 5 4 3 2 1
s1 : 4 3 2 1
s1 : 3 2 1
s1 :
s2 : 3 2 1