前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >STL_queue

STL_queue

作者头像
GeekLiHua
发布2025-01-21 13:51:27
发布2025-01-21 13:51:27
3500
代码可运行
举报
文章被收录于专栏:JavaJava
运行总次数:0
代码可运行

STL_queue

queue的基本概念

一般的queue容器只能队尾进,队首出,双向队列deque那就是另一回事儿了。

queue常用接口

一段代码学会队列的使用

代码语言:javascript
代码运行次数:0
复制
#include<iostream>
#include<queue>

using namespace std;

// 队列 Queue

class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
void test01()
{
	// Create queue
	// 创建一个队列
	queue<Person>q;

	// Data preparation
	// 准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙僧", 800);

	// Push
	// 往队列里面添加元素
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

	cout << "队列大小为:" << q.size() << endl;

	// Judge whether the queue is empty or not, check the opposite head, check the end of the queue, and exit the queue
	// 判断队列是否为空,然后检查对首和队尾,最后退出队列
	while (!q.empty())
	{
		// check the opposite head
		// 查看对首元素
		cout << "对头元素 --- 姓名:" << q.front().m_Name << "年龄:" << q.front().m_Age << endl;

		// check the end of the queue
		// 查看队尾元素
		cout << "队尾元素 --- 姓名:" << q.back().m_Name << "年龄:" << q.front().m_Age << endl;
		cout << endl;
		// 出队列方法
		// out of queue
		q.pop();
	}

	cout << "队列大小为:" << q.size() << endl;

}
int main()
{
	test01();
	return 0;
}

运行结果

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-01-20,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • STL_queue
    • queue的基本概念:
    • queue常用接口
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档