cb03a_c++_数据结构_顺序容器_STL_stack

/*cb03a_c++_数据结构_顺序容器_STL_stack
堆栈:LIFO--Last In First Out后进先出,用于系统程序设计
自适应容器(容器适配器),不是独立的容器,是一个适配器
栈适配器STL stack
stack<int,deque<int> s;
stack<int, vector<int>> s;
stack<int,list<int> s;
s.empty(),堆栈是否空
s.size();堆栈有多少个数据
s.pop();堆栈弹出数据
s.top();查看栈顶数据
s.push(item),数据压入堆栈

int x = d.pop(); //错误,pop删除数据,定义类型void,不返回。改为d.top();
error C2440: “初始化”: 无法从“void”转换为“int”
*/

 1 /*cb03a_c++_数据结构_顺序容器_STL_stack
 2 堆栈:LIFO--Last In First Out后进先出,用于系统程序设计
 3 自适应容器(容器适配器),不是独立的容器,是一个适配器
 4 栈适配器STL stack
 5 stack<int,deque<int> s;
 6 stack<int, vector<int>> s;
 7 stack<int,list<int> s;
 8 s.empey(),堆栈是否空
 9 s.size();堆栈有多少个数据
10 s.pop();堆栈弹出数据
11 s.top();查看栈顶数据
12 s.push(item),数据压入堆栈
13 
14 int x = d.pop(); //错误,pop删除数据,定义类型void,不返回。改为d.top();
15 error C2440: “初始化”: 无法从“void”转换为“int”
16 */
17 #include <iostream>
18 #include <stack>
19 #include <vector>
20 #include <list>
21 
22 using namespace std;
23 int main()
24 {
25     stack<int,deque<int>> a; //a堆栈用deque做的
26     stack<int, vector<int>> b; //b堆栈用vector做的
27     stack<int, list<int>> c; //c堆栈用list做的
28 
29     stack<int> d;//d,默认用的deque做的堆栈
30 
31     d.push(25);//把25压入堆栈
32     d.push(10);
33     d.push(1);
34     d.push(6);
35 
36     cout << "现在栈里面有多少个数据呢?: " << d.size() << endl;
37     
38     //int x = d.pop(); //错误,pop删除数据,定义类型void,不返回数据
39     int x = d.top();
40     d.pop();//删除数据
41     cout << x << endl;
42 
43     int x1 = d.top();
44     d.pop();//删除数据
45     cout << x1 << endl;
46     cout << "现在栈里面有多少个数据呢?: " << d.size() << endl;
47     //while(d.size()!=0)
48     while (d.empty() == false)
49     {
50         d.pop();//删除数据
51     }
52     cout << "现在栈里面有多少个数据呢?: " << d.size() << endl;
53     return 0;
54  }
欢迎讨论,相互学习。 txwtech@163.com
原文地址:https://www.cnblogs.com/txwtech/p/12305495.html