队列和优先队列

C++队列是一种容器适配器,它给予程序员一种先进先出(FIFO)的数据结构。
1.back() 返回一个引用,指向最后一个元素
2.empty() 如果队列空则返回真
3.front() 返回第一个元素
4.pop() 删除第一个元素
5.push() 在末尾加入一个元素
6.size() 返回队列中元素的个数

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
const int maxn=1007;
const int INF=0x3f3f3f3f;
using namespace std;

int main()
{
    queue<int>Q;
    Q.push(1);
    Q.push(5);
    printf("%d
", Q.front());
    return 0;
}

C++ Priority Queues(优先队列)
C++优先队列类似队列,但是在这个数据结构中的元素按照一定的断言排列有序。
1.empty() 如果优先队列为空,则返回真
2.pop() 删除第一个元素
3.push() 加入一个元素
4.size() 返回优先队列中拥有的元素的个数
5.top() 返回优先队列中有最高优先级的元素

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
const int maxn=1007;
const int INF=0x3f3f3f3f;
using namespace std;

priority_queue<int>Q;

int main()
{
    for(int i=0; i<4; i++)
        Q.push(i);

    while(!Q.empty())
    {
        int x=Q.top();
        Q.pop();
        printf("%d
", x);
    }
    return 0;
}

#include<iostream>
#include<cstdio>
#include<cstring>
#include<stack>
#include<queue>
#include<algorithm>
const int maxn=1007;
const int INF=0x3f3f3f3f;
using namespace std;

priority_queue<int>Q;

int main()
{
for(int i=0; i<4; i++)
Q.push(i);

while(!Q.empty())
{
int x=Q.top();
Q.pop();
printf("%d ", x);
}
return 0;
}

原文地址:https://www.cnblogs.com/w-y-1/p/6705078.html