C++黑马程序员——P213-214. queue容器

发布时间 2023-05-25 14:32:01作者: 我会变强的
  • P213. queue容器——基本概念
  • P214. queue容器——常用接口
  • P213. queue容器 基本概念

  

 

  • P214. queue 常用接口

  

示例

 1 class Person
 2 {
 3 public:
 4     Person(string name, int age){
 5         this->m_Name = name;
 6         this->m_Age = age;
 7     }
 8     string m_Name;
 9     int m_Age;
10 };
11 
12 void test01() {
13     //创建队列
14     queue<Person>q;
15 
16     //准备数据
17     Person p1("aaa", 10);
18     Person p2("bbb", 20);
19     Person p3("ccc", 30);
20     Person p4("ddd", 40);
21 
22     //入队
23     q.push(p1);
24     q.push(p2);
25     q.push(p3);
26     q.push(p4);
27 
28     //只要队列不为空,查看队头,查看队尾,出队
29     while (!q.empty()) {
30         //查看队头
31         cout << q.front().m_Name << "\t" << q.front().m_Age << endl;
32         //查看队尾
33         cout << q.back().m_Name << "\t" << q.back().m_Age << endl;
34         //查看队列大小
35         cout << q.size() << endl;
36         //出队(出队头)
37         q.pop();
38         cout << endl;
39     }
40 }
41 
42 
43 int main() {
44     test01();
45     return 0;
46 }

运行结果:

  

总结:

  

(〃>_<;〃)(〃>_<;〃)(〃>_<;〃)