C++ queue队列模型基本操作
#include <iostream>
#include <queue>
using namespace std;
class Teacher
{
public:
Teacher(int age)
{
this->age = age;
}
void printT()
{
cout << "age: " << age << endl;
}
private:
int age;
char name[32];
};
int main()
{
queue<int> q;
for (int i = 0; i < 10; i++)
{
q.push(i + 1);
}
cout << "队头元素:" << q.front() << endl;
cout << "队列大小:" << q.size() << endl;
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
cout << endl;
Teacher t1(11), t2(22), t3(33);
queue<Teacher> q2;
q2.push(t1);
q2.push(t2);
q2.push(t3);
while (!q2.empty())
{
q2.front().printT();
q2.pop();
}
queue<Teacher*> q3;
q3.push(&t1);
q3.push(&t2);
q3.push(&t3);
while (!q3.empty())
{
q3.front()->printT();
q3.pop();
}
return 0;
}