queue的用法

点击查看代码
#include<cstdio>
#include<queue>
#pragma warning(disable:4996)
using namespace std;

int main() {
	queue<int> q; //定义队列q,存储int型元素
	for (int i = 1; i <= 5; i++) {
		q.push(i); //将i存入队列中,q:1 2 3 4 5
	}
	//访问queue容器的元素只能使用front()和back(),分别返回队列首尾元素的值
	printf("%d %d\n", q.front(), q.back()); //输出1 5

	q.pop(); //将q的首元素弹出,即删除q的首元素1
	printf("%d\n", q.front()); //q的首元素变为2,q:2 3 4 5

	while (q.empty()==false) { //删除q中所有元素
		q.pop();
	}
	printf("%d", q.size()); //删除q中所有元素后,q内元素个数为0

	return 0;
}

posted @ 2022-09-30 23:02  zhaoo_o  阅读(23)  评论(0编辑  收藏  举报