zoukankan      html  css  js  c++  java
  • c++stack容器介绍

    c++stack(堆栈)是一个容器的改编,它实现了一个先进后出的数据结构(FILO)

    使用该容器时需要包含#include<stack>头文件;

    定义stack对象的示例代码如下:

    stack<int>s1;

    stack<string>s2;

    stack的基本操作有:

    1.入栈:如s.push(x);

    2.出栈:如 s.pop().注意:出栈操作只是删除栈顶的元素,并不返回该元素。

    3.访问栈顶:如s.top();

    4.判断栈空:如s.empty().当栈空时返回true。

    5.访问栈中的元素个数,如s.size();

    下面举一个简单的例子:

    #include<iostream>  
    #include<stack>  
    using namespace std;  
    int main(void)  
    {  
        stack<double>s;//定义一个栈  
        for(int i=0;i<10;i++)  
            s.push(i);  
        while(!s.empty())  
        {  
            printf("%lf
    ",s.top());  
            s.pop();  
        }  
        cout<<"栈内的元素的个数为:"<<s.size()<<endl;  
        return 0;  
    }
  • 相关阅读:
    if
    C#
    C#
    C#
    .net 5.0
    .net 5.0
    .net 5.0
    设计模式
    GAN网络中采用导向滤波的论文
    pytorch~多loss的选择
  • 原文地址:https://www.cnblogs.com/AquaGot/p/7241852.html
Copyright © 2011-2022 走看看