By
yusijia
Updated:
Contents
- 单向队列 queue支持
- empty()
- size()
- front()
- back()
- push()
- pop()
由于queue只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的。下面给出单向队列的使用范例:
参考:http://blog.csdn.net/MoreWindows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <queue> #include <vector> #include <list> #include <cstdio> using namespace std; int main() { queue<int, list<int>> a; queue<int> b; int i; for (i = 0; i < 10; i++) { a.push(i); b.push(i); } printf("%d %d\n", a.size(), b.size()); printf("%d %d\n", a.front(), a.back()); printf("%d %d\n", b.front(), b.back()); while (!a.empty()) { printf("%d ", a.front()); a.pop(); } putchar('\n'); while (!b.empty()) { printf("%d ", b.front()); b.pop(); } putchar('\n'); return 0; }
|