c++queue容器

queue 模板类的定义在<queue>头文件中。
与stack 模板类很相似,queue 模板类也需要两个模板参数,一个是元素类型,一个容器类
型,元素类型是必要的,容器类型是可选的,默认为deque 类型。
定义queue 对象的示例代码如下:
queue<int> q1;
queue<double> q2;

queue 的基本操作有:
入队,如例:q.push(x); 将x 接到队列的末端。
出队,如例:q.pop(); 弹出队列的第一个元素,注意,并不会返回被弹出元素的值。
访问队首元素,如例:q.front(),即最早被压入队列的元素。
访问队尾元素,如例:q.back(),即最后被压入队列的元素。
判断队列空,如例:q.empty(),当队列空时,返回true。
访问队列中的元素个数,如例:q.size()

 1 #include<iostream>
 2 using namespace std;
 3 #include<queue>
 4 #include<algorithm>
 5 
 6 //队列中基本数据模型
 7 void main61()
 8 {
 9     queue<int> q;
10     q.push(1);
11     q.push(2);
12     q.push(3);
13     q.push(4);
14 
15 
16     cout << "队头元素:" << q.front() << endl;
17     cout << "队列大小:" << q.size() << endl;
18 
19     while (!q.empty())
20     {
21         cout << q.front() << " ";
22         q.pop();
23 
24     }
25 }
26 
27 //队列的算法 和 数据类型的分离
28 class Teacher
29 {
30 public:
31     int age;
32     char name[32];
33 
34 public:
35     void printT()
36     {
37         cout << "age: " << age << endl;
38     }
39 };
40 //队列中对象数据模型
41 void main62()
42 {
43     Teacher t1, t2, t3;
44     t1.age = 31;
45     t2.age = 32;
46     t3.age = 33;
47     queue<Teacher> q;
48     q.push(t1);
49     q.push(t2);
50     q.push(t3);
51 
52     while (!q.empty())
53     {
54         Teacher temp = q.front();
55         temp.printT();
56         q.pop();
57     }
58 
59 
60 
61 }
62 //队列中指针数据模型
63 void main63()
64 {
65     Teacher t1, t2, t3;
66     t1.age = 31;
67     t2.age = 32;
68     t3.age = 33;
69     queue<Teacher *> q;
70     q.push(&t1);
71     q.push(&t2);
72     q.push(&t3);
73 
74     while (!q.empty())
75     {
76         Teacher *temp = q.front();
77         temp->printT();
78         q.pop();
79     }
80 
81 
82 
83 }
84 int main()
85 {
86     main63();
87 
88 
89 
90     system("pause");
91     return 0;
92 }
原文地址:https://www.cnblogs.com/ymj11/p/13810190.html