STL queue队列

queue

empty(判断队列是否为空)
Test whether container is empty (public member function )
size(计算队列大小)
Return size (public member function )
front(队列头的元素)
Access next element (public member function )
back(队列尾的元素)
Access last element (public member function )
push (从队尾插入元素)
Insert element (public member function )
emplace (从队尾插入元素)
Construct and insert element (public member function )
pop (从队头弹出元素)
Remove next element (public member function )
swap
Swap contents (public member function )

#include <iostream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MAXN = 10005;
typedef long long LL;
const LL mod = 998244353;
const LL inf = 0x3f3f3f3f;
queue<int>q,p;
int main()
{
    if(q.empty())
        printf("队列为空\n");
    q.push(1);
    q.push(2);
    if(q.empty())
        printf("队列为空\n");
    else
        printf("队列q有%d个元素\n", q.size());
    p.push(3);
    p.push(4);
    q.swap(p);
    printf("%d %d %d %d\n", q.front(), q.back(), p.front(), p.back());
    return 0;
}

priority_queue

#include <iostream>
#include <cmath>
#include <cctype>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
#include <map>
//#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const int MAXN = 10005;
typedef long long LL;
const LL mod = 998244353;
const LL inf = 0x3f3f3f3f;
//priority_queue<int> q;//默认从大到小
//priority_queue<int, vector<int>, greater<int>> q;//从小到大
//priority_queue<int, vector<int>, less<int>> q;//从大到小
struct number
{
    int x;
    bool operator < (const number &a)const
    {
        return x>a.x;//从小到大
    }
};
priority_queue<number> q;
struct cmp
{
    bool operator ()(int &a, int &b)
    {
        return a>b;//从小到大
    }
};
priority_queue<int, vector<int>, cmp> p;
int main()
{
    number a,b,c;
    a.x=1;
    b.x=3;
    c.x=2;
    q.push(a);
    q.push(b);
    q.push(c);
    int cnt=0;
    while(!q.empty())
    {
        number x=q.top();
        q.pop();
        printf("第%d个元素是%d\n", ++cnt, x.x);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值