cpp拾遗——STL——queue

1. 特点

  • 先进先出
  • 使用deque实现

2. 默认构造

queue采用模板类实现,queue对象的默认构造形式:queue queT; 如:
queue queInt; //一个存放int的queue容器。
queue queFloat; //一个存放float的queue容器。
queue queString; //一个存放string的queue容器。
...
//尖括号内还可以设置指针类型或自定义类型。

3. push pop

queue.push(elem); //往队尾添加元素
queue.pop(); //从队头移除第一个元素

queue queInt;
queInt.push(1);queInt.push(3);
queInt.push(5);queInt.push(7);
queInt.push(9);queInt.pop();
queInt.pop();
此时queInt存放的元素是5,7,9

4. 拷贝构造与赋值

queue(const queue &que); //拷贝构造函数
queue& operator=(const queue &que); //重载等号操作符

		queue<int> queIntA;
		queIntA.push(1);
		queIntA.push(3);
		queIntA.push(5);
		queIntA.push(7);
		queIntA.push(9);

		queue<int> queIntB(queIntA);	//拷贝构造
		queue<int> queIntC;
		queIntC = queIntA;				//赋值

5. 存取

queue.back(); //返回最后一个元素
queue.front(); //返回第一个元素

		queue<int> queIntA;
		queIntA.push(1);
		queIntA.push(3);
		queIntA.push(5);
		queIntA.push(7);
		queIntA.push(9);

		int iFront = queIntA.front();		//1
		int iBack = queIntA.back();		//9

		queIntA.front() = 11;			//11
		queIntA.back() = 19;			//19

6. 大小

queue.empty(); //判断队列是否为空
queue.size(); //返回队列的大小

		queue<int> queIntA; 	
		queIntA.push(1);   	
		queIntA.push(3);  		
		queIntA.push(5);		
		queIntA.push(7);		
		queIntA.push(9);		

		if (!queIntA.empty())
		{
			int iSize = queIntA.size();		//5
		}

posted on   开心种树  阅读(132)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示