1.基本概念
queue是一种先进先出的数据结构,它有两个出口。
2.常用接口
演示:
#include <iostream>
#include <queue>
#include <string>
class Person
{
public:
Person(string name, int age)
{
this->m_ Name = name;
this->m_ Agq =age;
}
string m_ Name;
int m_ Age;
};
void test01() {
//创建队列
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().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() {
test01();
system("pause");
return 0;
}