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;
}