栈 <stack>

STL:

 

pop:完成的仅仅是移除最顶端的数据.如果访问最顶端的数据,需要使用top函数(这个操作通常也被称为peek).

 

 1 #include <stack>
 2 #include <cstdio>
 3 uisng namespace std;
 4 
 5 int main()
 6 {
 7     stack<int> s;  //  声明存储int类型数据的栈
 8     s.push(1);     //  {}--{1}
 9     s.push(2);     //  {1}--{1,2}
10     s.push(3);     //  {1,2}--{1,2,3}
11     printf("%d\n",s.top());  // 3
12     s.pop();       //  从栈顶移除{1,2,3}--{1,2}
13     printf("%d\n",s.top());  // 2
14     s.pop();       //  {1,2}--{1}
15     printf("%d\n",s.top());  // 1
16     s.pop();       //  {1}--{}
17     return 0;
18 }

 

 

<<挑战程序设计竞赛>>读后感

posted @ 2016-02-27 12:05  Vmetrio  阅读(155)  评论(0编辑  收藏  举报