By
yusijia
Updated:
stack
栈只是进一步封装别的数据结构,并提供自己的接口,所以代码非常简洁,如果不指定容器,默认是用deque来作为其底层数据结构的。下面给出栈的使用范例:
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
|
#include <stack> #include <vector> #include <list> #include <cstdio> using namespace std; int main() { stack<int, list<int>> a; stack<int, vector<int>> b; int i;
for (i = 0; i < 10; i++) { a.push(i); b.push(i); }
printf("%d %d\n", a.size(), b.size());
while (!a.empty()) { printf("%d ", a.top()); a.pop(); } putchar('\n');
while (!b.empty()) { printf("%d ", b.top()); b.pop(); } putchar('\n'); return 0; }
|