STL12-queue容器

20 篇文章 0 订阅
20 篇文章 1 订阅

queue容器 队列容器 先进先出

队列只能在一端插入 一端删除

队列不能遍历 不提供迭代器 不支持随机访问

#if 1
#include<iostream>
#include<queue>
using namespace std;
void test01() {
	queue<int> q; //创建队列
	queue<int> q2(q);
	q.push(10);
	q.push(20);
	q.push(30);
	q.push(40);
	cout << "队尾:"<<q.back() << endl;
	cout << "队头:"<<q.front() << endl;
	while (!q.empty()) {
		cout << q.front() << endl;
		q.pop(); //删除队头
	}
}
//作业1 queue容器存放对象指针
//作业2 queue容器存放stack容器
int main()
{
	test01();
	return 0;
}
#endif

作业:

#if 1
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
class Person {
public:
	Person(int Age, int Id) :age(Age), id(Id) {}
public:
	int age;
	int id;
};
//作业1 queue容器存放对象指针
void test01() {
	queue<Person*> q;
	Person p1 = { 23,1 };
	Person p2 = { 24,2 };
	Person p3 = { 25,3 };
	q.push(&p1);
	q.push(&p2);
	q.push(&p3);
	while (q.size() > 0) {
		Person *p = q.front();
		cout << (*p).age << " " << (*p).id << endl;
		q.pop();
	}

}
//作业2 queue容器存放stack容器
void test02() {
	stack<Person> sp1;
	Person p1 = { 23,1 };
	Person p2 = { 24,2 };
	Person p3 = { 25,3 };
	sp1.push(p1);
	sp1.push(p2);
	sp1.push(p3);
	stack<Person> sp2 = sp1;
	queue<stack<Person>> q;
	q.push(sp1);
	q.push(sp2);
	cout << "queue中stack数为:" << q.size() << endl;
	while (!q.empty()) {
		stack<Person> s=q.front();
		cout << "stack数据内容为:" << endl;
		while (!s.empty()) {
			Person p = s.top();
			s.pop();
			cout << p.age << " " << p.id << endl;
		}
		q.pop();
		
	}
}

int main()
{
	cout << "test01:queue容器存放对象指针" << endl;
	test01();
	cout << "test02:queue容器存放stack容器" << endl;
	test02();
	return 0;
}
#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chde2Wang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值