Contents
  1. 1. stack

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

//栈 stack支持 empty() size() top() push() pop()
#include <stack>
#include <vector>
#include <list>
#include <cstdio>
using namespace std;
int main()
{

//可以使用list或vector作为栈的容器,默认是使用deque的。
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;
}
Contents
  1. 1. stack