赞
踩
- #include <iostream>
- #include <stack>
- #include <list>
- #include <queue>
- #include <functional>
- #include <iterator>
-
- using namespace std;
-
- int main() {
- // 栈(先进后出filo)
- stack<int, list<int>> s;
- for(int i = 0; i < 5; ++i)
- {
- s.push(i);
- }
-
- // 不能用下面的这种循环,因为pop后,size会发生变化,输出4 3 2
- // for(size_t i = 0; i < s.size(); ++i)
- // {
- // cout << s.top() << " ";
- // s.pop();
- // }
-
- while (!s.empty())
- {
- cout << s.top() << " ";
- s.pop();
- }
- cout << endl;
-
- // 队列(先进先出)
- queue<int, list<int>> q; // 注意这边的list不能用vector,因为源码中vec没有pop_front接口,所以要注意接口的匹配
- for (int i = 0; i < 5; ++i)
- {
- q.push(i);
- }
-
- while (!q.empty())
- {
- cout << q.front() << " ";
- q.pop();
- }
- cout << endl;
-
- // 优先级队列(不一定先进先出)
- int a[] = {5, 1, 3, 2, 4};
- priority_queue<int> pq(a, a + 5); // 这里面会调用make_heap
- while (!pq.empty())
- {
- cout << pq.top() << " "; // 弹出是按照值的大小,值越大,优先级越高
- pq.pop();
- }
- cout << endl;
-
- // 堆(二叉树,大堆或者小堆)
- make_heap(a, a + 5);
-
- copy(a, a + 5, ostream_iterator<int>(cout, " "));
- cout << endl;
-
- sort(a, a + 5, less<int>());// 默认是greater大堆
- copy(a, a + 5, ostream_iterator<int>(cout, " "));
- cout << endl;
-
- return 0;
- }
-
- //输出
- 4 3 2 1 0
- 0 1 2 3 4
- 5 4 3 2 1
- 5 4 3 2 1
- 1 2 3 4 5

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。