黑马:queue容器(213~214)

本文演示了C++中使用`queue`容器实现数据结构队列的基本操作,包括入队、出队、查看队头和队尾元素以及判断队列是否为空和获取队列大小。通过创建Person类实例,展示了队列在存储和处理对象时的应用。
摘要由CSDN通过智能技术生成

1.基本概念

 queue是一种先进先出的数据结构,有两个出口

2.queue常用接口

#include<iostream>
using namespace std;
#include<string>
#include<vector>
#include<deque>
#include<iterator>
#include<list>
#include<algorithm>  //标准算法头文件
#include<numeric>
#include<map>
#include<set>
#include<utility>
#include<fstream>
#include<string>
#include<ctime>
#include<stack>
#include<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() {
	queue<Person>q;

	Person p1("唐僧", 30);
	Person p2("悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙和尚", 700);

	//入队
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

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

	//判断只要队列不为空,查看队头,查看队尾,出队
	while (!q.empty()) {
		//查看队头
		cout << "队头元素--姓名: " << q.front().m_Name << " 年龄: " << q.front().m_Age << endl;

		//查看队尾
		cout << "队尾元素--姓名: " << q.back().m_Name << " 年龄: " << q.back().m_Age << endl;

		cout << endl;

		//出队
		q.pop();
	}

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




int main(int argc,char**argv) {

	test01();
	
	system("pause");
	return  0;
}




总结:

入队--push

出队--pop

返回队头元素--front

返回队尾元素--back

判断是否为空--empty

返回队列大小--size

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值