队列:Queue
- 定义一个队列: queue< typename > q。
- queue是不能使用下标访问的。只能使用q.front() 和 q.back() 来访问。
- 如果想要访问所有的数值,可以不断使用pop(),弹出第一个数字,然后再访问 front()。
- 判断符号是否为空,如果回答是(True),则是空的。
- q.size()可以求得长度。
- 注意要点:在使用front 和 pop之前必须判断是否为空,否则报错。
#include<iostream>
#include<queue>
using namespace std;
queue<int> q;
int main() {
for(int i = 0 ; i < 5; i ++) {
q.push(i);
}
printf("%d\n" , q.front());
q.pop();
printf("%d\n" , q.front());
if(q.empty() == true) {
printf("Empty\n");
}
else printf("Not empty!\n");
printf("%d\n" , q.size());
return 0;
}