本节及上节思维导图
上节内容【C++】容器Vector、String、Deque
Stack容器
- 先进后出(FILO)
- 不允许遍历
#include<iostream>
using namespace std;
#include<algorithm>
#include<stack>
#include<queue>
void test() {
//构造栈
stack<int> s;
//入栈
s.push(1);
s.push(2);
s.push(3);
s.push(4);
while (!s.empty()) {//不为空时
cout << s.top() << endl;//查看栈顶元素
s.pop();//出栈
}
cout << s.size() << endl;
}
int main() {
test();
system("pause");
return 0;
}
Queue容器
- 先进先出FIFO
- 允许一端新增数据(入队),另一端删除数据(出队)
- 不允许遍历
#include<iostream>
using namespace std;
#include<algorithm>
#include<queue>
void test() {
//构造队列
queue<int> q;
//入队
q.push(1);
q.push(2);
q.push(3);
q.push(4);
while (!q.empty()) {
cout << q.front() << endl;//查看队头元素
cout << q.back() << endl;//查看队尾元素
q.pop();//出队
}
cout << q.size() << endl;
}
int main() {
test();
system("pause");
return 0;
}