C++:queue

queue 在C++中是队列
使用头文件#include< queue >;
要使用首先要学会 数据结构中的队列

push

向队尾添加数据

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(2);
}

front

返回队列中第一进入的元素

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(2);
    int i=Q.front();
    printf("%d",i);
}

输出结果为1。

pop

删除队列中最前面的元素

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(2);
    Q.pop();
    int i=Q.front();
    printf("%d",i);
}

输出的结果是2;

empty

判断队列是否为空 如果为空返回 true

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(2);
    Q.pop();
   cout<<Q.empty()<<endl;
   Q.pop();
   cout<<Q.empty()<<endl;
}

输出结果:

0
1

back

返回队列中最后一个进入的元素,并不删除

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(2);
   int i=Q.back();
   cout<<i;
}

输出结果为2;

size

返回队列中元素个数 注意是个数。

#include<bits/stdc++.h>
using namespace std;
int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(2);
   
     cout<<Q.size();
}

输出结果为2。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值