queue的交换,赋值以及初始化

 1 #include<iostream>
 2 #include<queue>
 3 using namespace std;
 4 struct dian{
 5     int x;
 6     int y;
 7 };
 8 /* 为了说明queue里面的数据类型可以通用,随便定义了一个结构体*/
 9 void print(queue<dian> &q)
10 {
11     while(!q.empty())
12     {
13         cout<<q.front().x<<" "<<q.front().y<<"  ";
14         q.pop();
15     }
16 } 
17 void swap(queue<dian> &q1,queue<dian> &q2)
18 {
19     queue<dian> q3;
20     q3=q1;
21     q1=q2;
22     q2=q3;
23 }
24 int main()
25 {
26     dian a;
27     a.x=6;
28     a.y=7;
29     queue<dian> q1;
30     queue<dian> q2;
31     q1.push(a);
32     for(int i=0;i<10;i++)
33     {
34         q2.push(a);
35     }
36     queue<dian> q3(q2);
37 //    q1=q2;
38 //    q1.swap(q2);
39     swap(q1,q2);
40 //    print(q1);
41 //    cout<<endl; 
42 //    print(q2);
43     print(q3);
44 } 
45 /* 队列可以直接交换,也可以直接一个队列等于另一个队列*/

猜你喜欢

转载自www.cnblogs.com/dayq/p/11938905.html