queue 在C++中是队列
使用头文件#include< queue >;
要使用首先要学会 数据结构中的队列
push
向队尾添加数据
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int>Q;
Q.push(1);
Q.push(2);
}
front
返回队列中第一进入的元素
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int>Q;
Q.push(1);
Q.push(2);
int i=Q.front();
printf("%d",i);
}
输出结果为1。
pop
删除队列中最前面的元素
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int>Q;
Q.push(1);
Q.push(2);
Q.pop();
int i=Q.front();
printf("%d",i);
}
输出的结果是2;
empty
判断队列是否为空 如果为空返回 true
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int>Q;
Q.push(1);
Q.push(2);
Q.pop();
cout<<Q.empty()<<endl;
Q.pop();
cout<<Q.empty()<<endl;
}
输出结果:
0
1
back
返回队列中最后一个进入的元素,并不删除
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int>Q;
Q.push(1);
Q.push(2);
int i=Q.back();
cout<<i;
}
输出结果为2
;
size
返回队列中元素个数 注意是个数。
#include<bits/stdc++.h>
using namespace std;
int main()
{
queue<int>Q;
Q.push(1);
Q.push(2);
cout<<Q.size();
}
输出结果为2。