C++ //栈 stack 容器 先进后出 不允许遍历

 1 //栈 stack 容器  先进后出  不允许遍历
 2  
 3 
 4 #include<iostream>
 5 #include<stack>
 6 
 7 using namespace std;
 8 
 9 
10 void test01()
11 {
12     //特点 先进后出数据结构
13     stack<int>s;
14 
15     //入栈
16     s.push(10);
17     s.push(20);
18     s.push(30);
19     s.push(40);
20 
21     cout << "栈的大小:" << s.size() << endl;
22     //只要栈不为空 就查看栈顶 并且执行出栈操作
23     while (!s.empty())
24     {
25         //查看栈顶的元素
26         cout << "栈顶元素为: " << s.top() << endl;
27 
28         //出栈
29         s.pop();
30     }
31     cout << "栈的大小:" << s.size() << endl;
32 
33 }
34 int main()
35 {
36 
37     test01();
38 
39     system("pause");
40     return 0;
41 
42 }

 

posted on 2021-08-15 12:58  Bytezero!  阅读(227)  评论(0编辑  收藏  举报