队列和优先队列

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\n", 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\n", 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\n", x);
}
return 0;
}

时间: 2024-08-01 11:55:14

队列和优先队列的相关文章

【C/C++学院】0802-链式栈/链表队列以及优先队列/封装链表库

链式栈 // stacklinknode.h #define datatype int struct stacknode { int num;//编号 datatype data;//数据 struct stacknode *pNext;//指针域 }; typedef struct stacknode StackNode;//简化 StackNode * init(StackNode * phead);//初始化 StackNode * push(StackNode * phead, int

ZOJ 2724 Windows 消息队列 (优先队列)

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2724 Message queue is the basic fundamental of windows system. For each process, the system maintains a message queue. If something happens to this process, such as mouse click, text cha

STL栈、队列、优先队列—————基础知识

0基本特点:后进先出(LIFO) 注意: 不一定最先进栈的最后出栈,只要保证是栈顶元素出栈就行! 当栈中存在一个元素时,top=0,因此通常把空栈的判定条件定为top= - 1: STL 中栈的使用方法: 头文件:#include <stack> 基本操作: push(x) 将x加入栈中,即入栈操作 pop() 出栈操作(删除栈顶),只是出栈,没有返回值 top() 返回第一个元素(栈顶元素) size() 返回栈中的元素个数 empty() 当栈为空时,返回 true STL 中队列的使用(

STL 队列 、优先队列、栈 小结

学长说现在基本上可以开始学习STL中一些标准模板了,今天先总结一下 队列.栈.优先队列 1.队列(queue) 先进先出原则,头文件#include <queue>,定义结构queue<类型>名称;queue<int>q.queue<node>q等: 如: struct node { int x; }f; queue<node>q;//结构体类型队列 q.push(f) //将f压入队列的尾部 node t = q.pop()// 弹出队列的第一

STL队列、优先队列、栈

STL 中优先队列的使用方法(priority_queu) 基本操作: empty() 如果队列为空返回真 pop() 删除对顶元素 push() 加入一个元素 size() 返回优先队列中拥有的元素个数 top() 返回优先队列对顶元素 在默认的优先队列中,优先级高的先出队.在默认的int型中先出队的为较大的数. 使用方法: 头文件: #include <queue> 声明方式: 1.普通方法: priority_queue<int>q;//通过操作,按照元素从大到小的顺序出队

栈,队列与优先队列

STL提供3种特殊的数据结构:栈,队列与优先队列 1.栈:符合"后进后出",有push和pop两种操作 其中push把元素压入栈顶,而pop从栈顶把元素"弹出".头文件<stack> 声明栈:stack<int>s; 2.优先队列:是一种抽象数据类型,行为有些像队列,但先进队列的元素不是先进队列的元素,而是队列中优先级最高的元素,这样就可以允许类似于"急诊病人插队"这样的事件发生. 头文件:#include<queu

数据结构-队列,优先队列

队列是遵循先进先出(First-In-First-Out)模式的线性表. 一般有以下操作: boolean add(E e); Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning true upon success and throwing an IllegalStateExce

UVA 12100 Printer Queue(队列和优先队列,水)

1 //#include<bits/stdc++.h> 2 #include<cstdio> 3 #include<queue> 4 #include<algorithm> 5 using namespace std; 6 typedef long long ll; 7 /** 8 题意:所有任务在队列中,若当前打印的任务优先级不是最大,则移动到队列尾部.问下标为k的任务在什么时刻被打印: 9 思路:用优先队列判断优先级是否最高.用队列模拟任务 10 (1)

js模拟队列----小优先队列

队列:先进先出,后进后出 var Queue = (function(){ var item = new WeakMap(); class Queue{ constructor(){ item.set(this,[]); } enqueue(ele,priority){ var ls = item.get(this); var obj = { ele:ele, pro:priority }; var add = false; for(var i = 0; i < ls.length; i++ )