queue的常用操作
原创
©著作权归作者所有:来自51CTO博客作者小怪兽会微笑的原创作品,请联系作者获取转载授权,否则将追究法律责任
欢迎访问我的STL库介绍
本文介绍常用的PAT里STL库queue的使用,对付PAT考试或其他上机要求足够了
声明
queue<int> q;//声明了一个int队列
queue<string> q;//声明了一个string队列
//当然也可以放入结构体
#include <bits/stdc++.h>
using namespace std;
struct person{
string name;
int id;
}per;
int main(){
queue<person> q;
per.id=1;
per.name="jess";
q.push(per);
person newper=q.front();
cout<<newper.id<<" "<<newper.name;
return 0;
}
//输出1 jess
a.push()
a.front()
a.pop()
a.size()
a.empty()
a.empty();//是空返回true,否则返回false
使用queue进行广度优先搜索BFS例子
void BFS(int root){//静态树
queue<int> q;
q.push(root);
while(!q.empty()){
int now=q.front();
q.pop();
printf("%d",now);
if(Node[now].lchild!=-1)q.push(Node[now].lchild);
if(Node[now].rchild!=-1)q.push(Node[now].rchild);
}
}