一.基础概念
相当于数据结构里面栈,后进先出,从尾部插入,从尾部删除。

二.基础用法
1.stack对象创建
1. 默认构造函数 | stack<int> stk1; |
2. 拷贝构造函数 | stack<int> stk2(stk1); |
2.stack赋值操作
stack<int> stk1;
stack<int> stk2;
stk1 = stk2;
3.stack入栈操作
stack<int> stk;
stk.push(5); // 5
stk.push(4); // 5 4
stk.push(3); // 5 4 3
stk.push(2); // 5 4 3 2
stk.push(1); // 5 4 3 2 1
4.stack获取栈顶元素
stack<int> stk;
stk.push(5); cout << stk.top() << endl; // 5
stk.push(4); cout << stk.top() << endl; // 4
stk.push(3); cout << stk.top() << endl; // 3
stk.push(2); cout << stk.top() << endl; // 2
stk.push(1); cout << stk.top() << endl; // 1
5.stack出栈操作
stack<int> stk;
stk.push(5); cout << stk.top() << endl; // 5
stk.push(4); cout << stk.top() << endl; // 4
stk.push(3); cout << stk.top() << endl; // 3
stk.push(2); cout << stk.top() << endl; // 2
stk.push(1); cout << stk.top() << endl; // 1
stk.pop(); cout << stk.top() << endl; // 2
stk.pop(); cout << stk.top() << endl; // 3
stk.pop(); cout << stk.top() << endl; // 4
stk.pop(); cout << stk.top() << endl; // 5
stk.pop(); cout << stk.top() << endl; // 为空会报错
5.stack大小操作
stk.empty() | 判断是否为空 |
stk.size() | 查看这个栈里有多少元素 |