文章目录
一. queue的基本概念
概念
二. 队列的常用接口
构造函数
赋值操作
数据存取
大小操作
/*----------------------------------------------------------------
* 项目: Classical Question
* 作者: Fioman
* 邮箱: geym@hengdingzhineng.com
* 时间: 2022/3/22
* 格言: Talk is cheap,show me the code ^_^
//----------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include<string>
#include<queue>
class Person
{
public:
Person(string name, int age)
{
this->mName = name;
this->mAge = age;
}
public:
string mName;
int mAge;
};
void test_01(void)
{
// 队列queue
queue<Person> q;
// 准备数据
Person p1("唐僧", 30);
Person p2("孙悟空", 1000);
Person p3("猪八戒", 900);
Person p4("沙僧", 800);
// 入队
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
// 判断只要队列不为空,查看队头,查看队尾,出队
while (!q.empty())
{
// 查看队头,队尾
cout << "队头: " << q.front().mName << " 年龄: " << q.front().mAge << endl;
cout << "队尾: " << q.back().mName << " 年龄: " << q.back().mAge << endl;
cout << "队列大小: " << q.size() << endl;
q.pop();
}
cout << "队列大小: " << q.size() << endl;
}
int main()
{
test_01();
system("pause");
return 0;
}
结果: