这是她 发表于 2020-6-18 20:32:32

C++旅程第14站——Stack

本帖最后由 这是她 于 2020-6-18 20:45 编辑

Difficulties strengthen the mind,as labor does the body.
                                                                Stack
Stack是一种先进后出的数据结构。

栈中只有顶端的元素才可以被外界使用,因此栈不允许有遍历行为。

#include<iostream>
#include<stack>

using namespace std;

void test1()
{
      //入栈——push
      //出栈——pop
      //返回栈顶——top
      //判断栈是否为空——empty
      //返回栈大小——size
      
      //符合先进后出
      stack<int> s1;
      
      //入栈
      s1.push(100);
      s1.push(200);
      s1.push(300);
      
      while (!s1.empty())
      {
                cout << "栈顶元素是: " << s1.top() << endl;
                s1.pop();
      }
      cout << "栈的大小是: " << s1.size() << endl;
      
}

int main()
{
      test1();
      
      return 0;
}
                                                                   {:10_311:}

页: [1]
查看完整版本: C++旅程第14站——Stack