STL--stack/queue

Stack:栈
概念:一种先进后出的容器
头文件为:#include<stack>
定义:</stack>

stack <int> st;
stack <string> ss;

成员函数与操作方法

empty()// 堆栈为空则返回真
pop() //移除栈顶元素
push() //在栈顶增加元素
size() //返回栈中元素数目
top() //返回栈顶元素

代码示例:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    stack<int>st;
    int m;
    int temp;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        st.push(temp);
        cout<<st.size()<<" "<<st.top()<<endl;
    }
    st.pop();
    cout<<st.top()<<endl;
    if(st.empty()!=true)
    {
        cout<<"yes"<<endl;
    }
    return 0;
}

结果如图所示:
图片说明

queue:队列
概念:具有先进先出特征的容器
头文件:#include<queue>
定义:</queue>

queue<int>q;
queue<string>q1;

成员函数与操作方法

q.push(x);  //  入队列
q.pop();    //  出队列
q.front();  //  访问队首元素
q.back();   //  访问队尾元素
q.empty();  //  判断队列是否为空
q.size();   //  访问队列中的元素个数

代码示例:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int> q;
    int m;
    int temp;
    cin>>m;
    for(int i=0;i<m;i++)
    {
        cin>>temp;
        q.push(temp);
        cout<<q.size()<<" "<<q.back()<<endl;
    }
    q.pop();
    cout<<q.front()<<endl;
    if(q.empty()!=true)
    {
        cout<<"yes"<<endl;
    }
    return 0;
}

结果如图所示:
图片说明

参考引用:stack/queue

全部评论

相关推荐

06-12 16:00
天津大学 Java
牛客30236098...:腾讯坏事做尽,终面挂是最破防的 上次被挂了后我连简历都不刷了
点赞 评论 收藏
分享
06-17 00:26
门头沟学院 Java
程序员小白条:建议换下项目,智能 AI 旅游推荐平台:https://github.com/luoye6/vue3_tourism_frontend 智能 AI 校园二手交易平台:https://github.com/luoye6/vue3_trade_frontend GPT 智能图书馆:https://github.com/luoye6/Vue_BookManageSystem 选项目要选自己能掌握的,然后最好能自己拓展的,分布式这种尽量别去写,不然你只能背八股文了,另外实习的话要多投,尤其是学历不利的情况下,多找几段实习,最好公司title大一点的
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务