c++stack容器和queue容器

#define _CRT_SECURE_NO_WARNINGS

#include <iostream>

#include <stack>

#include <queue>

using namespace std;

//stack翻译为栈,栈容器,先进后出

//栈不能遍历,不支持随机存取,只能通过top从栈顶获取和删除元素

//不提供迭代器,不能遍历,不支持随机存取

//queue翻译为队列,队列容器,先进先出

//不能进行遍历,不提供迭代器,不支持随机访问

//我们常用的容器就这两种不提供遍历

int main()

{

//初始化

stack<int> s1;

stack<int> s2(s1);

//stack操作

s1.push(10);

s1.push(20);

s1.push(30);

s1.pop();//删除栈顶元素

//打印栈容器的数据

while (!s1.empty())

{

cout << s1.top() << endl;//输出栈顶元素

s1.pop();//弹出栈顶元素,也就是队尾元素,栈容器只能读栈顶元素,想要去后面的,需要一个个弹出

}

//初始化

queue<int> q;

q.push(10);

q.push(20);

q.push(30);

//打印队列中的数据

while (!q.empty())

{

cout << q.front() << endl;//输出队头元素

cout << q.back() << endl;//输出队尾元素

q.pop();//弹出队头元素

}

return 0;

}

猜你喜欢

转载自blog.csdn.net/tulipless/article/details/81135413