链式队列数组队列优先队列以及非基本数据类型
1 // queue
2 #include<iostream>
3 #include<queue>
4 #include<list>
5 #include<deque>
6 #include<cstdlib>
7
8 using namespace std;
9
10 // queue stack 默认都是deque线性实现
11 // 可以强行指定list链式存储
12 // stack queue 都没有迭代器
13 void main()
14 {
15 //queue<char *> myq;// 队列不能{}初始化 {"calc","notepad"};
16 queue<char*,list<char*>> myq;// 链式存储
17 myq.push("calc");
18 myq.push("notepad");
19 myq.push("tasklist");
20 myq.push("pause");
21
22 while (!myq.empty())
23 {
24 char *p = myq.front();
25 system(p);
26 myq.pop();// 从头部开始弹出
27 }
28
29
30 cin.get();
31 }
32
33 //------------------------------------------------------------------------
34
35 void main()
36 {
37 // 自动排序
38 priority_queue<int> myq;
39 myq.push(10);
40 myq.push(13);
41 myq.push(11);
42 myq.push(18);
43 myq.push(12);
44
45 while (!myq.empty())
46 {
47 cout << myq.top() << endl;//
48 myq.pop();// 从头部开始弹出
49 }
50
51
52 cin.get();
53 }
54
55 //------------------------------------------------------------------------
56
57 struct Getmoney
58 {
59 char *path;
60 int money;
61 };
62
63 struct lessA
64 {
65 bool operator()(const Getmoney & money1,const Getmoney & money2)
66 {
67 return money1.money < money2.money;// 比较钱多少
68 }
69 };
70
71 void main()
72 {
73 // 自定义类型 存储的形式 仿函数
74 priority_queue<Getmoney,deque<Getmoney>,lessA> myq;
75
76 Getmoney getit[4] = {{"百度",10000},{"Google",5000},{"体彩",5000000},{"Facebook",100000}};
77
78 for(auto i : getit)
79 {
80 myq.push(i);
81 }
82
83 while (!myq.empty())
84 {
85 cout << myq.top().money << " " << myq.top().path << endl;//
86 myq.pop();// 从头部开始弹出
87 }
88
89
90
91 cin.get();
92 }
长风破浪会有时,直挂云帆济沧海
posted on 2015-06-14 14:28 Dragon-wuxl 阅读(141) 评论(0) 收藏 举报
浙公网安备 33010602011771号