STL之stack(栈)(4)

  • 1.先进后出,结构图
    这里写图片描述

  • 2.定义:

   stack<int>s1;
   stack<string>s2;
  • 3.基本操作
s.pop();//出栈操作只是删除栈顶的元素,并不返回该元素。
  • 4.代码举例:
#include<bits/stdc++.h>
using namespace std; 
int main()
{
     stack<int>s;
    for(int i=0;i<10;i++)s.push(i);
    cout<<"栈内的元素的个数为:"<<s.size()<<endl;
    while(!s.empty())
    {
        cout<<s.top()<<endl;
        s.pop();
    }
    cout<<"栈内的元素的个数为:"<<s.size()<<endl;
    return 0;
    //输出是:9 8 7 6 5 4 3 2 1 0 
}

猜你喜欢

转载自blog.csdn.net/zhaoshuling1109/article/details/80501171