欢迎访问我的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.push();//在队列末尾加入一个元素

a.front()

a.front();//返回第一个元素

a.pop()

a.pop();//弹出队列中第一个

a.size()

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