#include <iostream>
using namespace std;
#include<string>
#include<queue>
class Person {
public:
Person(string name, int age) {
this->name = name;
this->age = age;
}
string name;
int age;
};
void test() {
//特点:符合先进先出的数据结构
queue<Person> q;
//入队
q.push(Person("孙悟空", 100));
q.push(Person("猪八戒", 99));
q.push(Person("沙僧", 12));
q.push(Person("唐僧", 98));
//判断只要队列不为空,查看队头,查看队尾,出队
while (!q.empty()) {
//查看队头
cout << "队头元素 姓名:" << q.front().name << "年龄:" << q.front().age << endl;
//查看队尾
cout << "队尾元素 姓名:" << q.back().name << "年龄:" << q.back().age << endl;
//出队
q.pop();
}
cout << "队列的大小:" << q.size() << endl;
}
int main() {
test();
return 0;
}
c++中queue队列的常用接口
最新推荐文章于 2024-01-05 21:25:26 发布