java,java web,arduino开发版开发,MFC,C++,VC++,C#,机器视觉,OpenCV,创客学习交流。
txwtech@163.com

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.empey(),堆栈是否空

s.size();堆栈有多少个数据

s.pop();堆栈弹出数据

s.top();查看栈顶数据

s.push(item),数据压入堆栈


int x = d.pop(); //错误,pop删除数据,定义类型void,不返回。改为d.top();

error C2440: “初始化”: 无法从“void”转换为“int”

*/

#include <iostream>

#include <stack>

#include <vector>

#include <list>


using namespace std;

int main()

{

    stack<int,deque<int>> a; //a堆栈用deque做的

    stack<int, vector<int>> b; //b堆栈用vector做的

    stack<int, list<int>> c; //c堆栈用list做的


    stack<int> d;//d,默认用的deque做的堆栈


    d.push(25);//把25压入堆栈

    d.push(10);

    d.push(1);

    d.push(6);


    cout << "现在栈里面有多少个数据呢?: " << d.size() << endl;

    

    //int x = d.pop(); //错误,pop删除数据,定义类型void,不返回数据

    int x = d.top();

    d.pop();//删除数据

    cout << x << endl;


    int x1 = d.top();

    d.pop();//删除数据

    cout << x1 << endl;

    cout << "现在栈里面有多少个数据呢?: " << d.size() << endl;

    //while(d.size()!=0)

    while (d.empty() == false)

    {

        d.pop();//删除数据

    }

    cout << "现在栈里面有多少个数据呢?: " << d.size() << endl;

    return 0;

 }


评论

© txwtech笛科思 | Powered by LOFTER