队列(queue)

来源:互联网 发布:打阿里云的投诉电话 编辑:程序博客网 时间:2024/06/14 20:02

引言


队列是一种常用的数据结构,队列中的元素先进先出,只允许对队头和队尾操作


基本操作


头文件:#include<queue>

创建一个队列:queu<int> q

入队:q.push(x)

出队:q.pop()

访问队首元素:q.front()

访问队尾元素:q.back()

访问队列中的元素个数:q.empty()

为空则返回真:q.empty()


实战

#include<iostream>#include<queue>using namespace std;int main(){    queue<int> q;    q.push(1);    q.push(2);    q.push(3);    q.pop();    cout<<q.front()<<endl;    cout<<q.back()<<endl;    cout<<q.size()<<endl;    cout<<q.empty()<<endl;    return 0;}


练习场



 http://acm.hdu.edu.cn/showproblem.php?pid=1702 


原创粉丝点击