STL containers are pockets A dream ~
0, the access queue container element
#include <stdio.h>
#include <queue>
using namespace std;
int main(){
queue<int>q;
for(int i = 1;i <=5;i++){
q.push(i);
}
printf("%d %d\n",q.front(),q.back());
return 0;
}
1, commonly used functions
pop
#include <stdio.h>
#include <queue>
using namespace std;
int main(){
queue<int>q;
for(int i = 1;i <= 5;i++){
q.push(i);
}
for(int i = 1;i <= 3;i++){
q.pop();//不能写q.pop(i),因为弹出始终是队列头
}
printf("%d\n",q.front());
return 0;
}
empty()
#include <stdio.h>
#include <queue>
using namespace std;
int main(){
queue<int>q;
if(q.empty() == true){
printf("Empty\n");
}
else{
printf("Not Empty\n");
}
q.push(1);
if(q.empty() == true){
printf("Empty\n");
}
else{
printf("Not Empty\n");
}
return 0;
}
size () Returns the number of elements in the queue
#include <stdio.h>
#include <queue>
using namespace std;
int main(){
queue<int>q;
for(int i = 1;i <= 5;i++){
q.push(i);
}
printf("%d\n",q.size());
return 0;
}
Common uses: the realization of breadth-first search, you can not own a queue implemented manually, using a queue
addition, front () and pop () before the function, must be empty () to determine whether the queue is empty