queue容器 队列容器 先进先出
队列只能在一端插入 一端删除
队列不能遍历 不提供迭代器 不支持随机访问
#if 1
#include<iostream>
#include<queue>
using namespace std;
void test01() {
queue<int> q; //创建队列
queue<int> q2(q);
q.push(10);
q.push(20);
q.push(30);
q.push(40);
cout << "队尾:"<<q.back() << endl;
cout << "队头:"<<q.front() << endl;
while (!q.empty()) {
cout << q.front() << endl;
q.pop(); //删除队头
}
}
//作业1 queue容器存放对象指针
//作业2 queue容器存放stack容器
int main()
{
test01();
return 0;
}
#endif
作业:
#if 1
#include<iostream>
#include<queue>
#include<stack>
using namespace std;
class Person {
public:
Person(int Age, int Id) :age(Age), id(Id) {}
public:
int age;
int id;
};
//作业1 queue容器存放对象指针
void test01() {
queue<Person*> q;
Person p1 = { 23,1 };
Person p2 = { 24,2 };
Person p3 = { 25,3 };
q.push(&p1);
q.push(&p2);
q.push(&p3);
while (q.size() > 0) {
Person *p = q.front();
cout << (*p).age << " " << (*p).id << endl;
q.pop();
}
}
//作业2 queue容器存放stack容器
void test02() {
stack<Person> sp1;
Person p1 = { 23,1 };
Person p2 = { 24,2 };
Person p3 = { 25,3 };
sp1.push(p1);
sp1.push(p2);
sp1.push(p3);
stack<Person> sp2 = sp1;
queue<stack<Person>> q;
q.push(sp1);
q.push(sp2);
cout << "queue中stack数为:" << q.size() << endl;
while (!q.empty()) {
stack<Person> s=q.front();
cout << "stack数据内容为:" << endl;
while (!s.empty()) {
Person p = s.top();
s.pop();
cout << p.age << " " << p.id << endl;
}
q.pop();
}
}
int main()
{
cout << "test01:queue容器存放对象指针" << endl;
test01();
cout << "test02:queue容器存放stack容器" << endl;
test02();
return 0;
}
#endif