C++ Stack(堆棧) 是一個(gè)容器類的改編,為程序員提供了堆棧的全部功能,——也就是說實(shí)現(xiàn)了一個(gè)先進(jìn)后出(FILO)的數(shù)據(jù)結(jié)構(gòu)。
1.empty() 堆棧為空則返回真
2.pop() 移除棧頂元素
3.push() 在棧頂增加元素
4.size() 返回棧中元素?cái)?shù)目
5.top() 返回棧頂元素
棧可以用向量(vector)、線性表(list)或雙向隊(duì)列(deque)來實(shí)現(xiàn):
stack<vector<int>> s1;
stack<list<int> > s2;
stack<deque<int>> s3;
其成員函數(shù)有“判空(empty)” 、“尺寸(Size)” 、“棧頂元素(top)” 、“壓棧(push)” 、“彈棧(pop)”等。
范例引自:
http://blog.csdn.net/morewindows/article/details/6950881
1 int main()
2 {
3 //可以使用list或vector作為棧的容器,默認(rèn)是使用deque的。
4 stack<int, list<int>> a;
5 stack<int, vector<int>> b;
6 int i;
7
8 //壓入數(shù)據(jù)
9 for (i = 0; i < 10; i++)
10 {
11 a.push(i);
12 b.push(i);
13 }
14
15 //棧的大小
16 printf("%d %d\n", a.size(), b.size());
17
18 //取棧項(xiàng)數(shù)據(jù)并將數(shù)據(jù)彈出棧
19 while (!a.empty())
20 {
21 printf("%d ", a.top());
22 a.pop();
23 }
24 putchar('\n');
25
26 while (!b.empty())
27 {
28 printf("%d ", b.top());
29 b.pop();
30 }
31 putchar('\n');
32 return 0;
33 }
34
posted on 2012-06-05 13:16
王海光 閱讀(611)
評(píng)論(0) 編輯 收藏 引用 所屬分類:
STL