queue容器
queue是一种先进先出的数据结构,它有一个出口和一个入口,入口只能进,出口只能出。
queue同样不能遍历。
#include <queue>
#include <string>
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
void test1()
{
//创建队列
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);
cout << "队列大小:" << q.size() << endl;
//判断,只要队列不为空,查看对头、队尾,出队
while (!q.empty())
{
//对头
cout << "对头元素——姓名:" << q.front().m_Name << " 年龄:" << q.front().m_Age << endl;
//对尾
cout << "对尾元素——姓名:" << q.back().m_Name << " 年龄:" << q.back().m_Age << endl;
//出队
q.pop();
}
cout << "队列大小:" << q.size() << endl;
}
int main()
{
test1();
system("pause");
return 0;
}

查看8道真题和解析