标准模板库(STL)之 queue 用法【初级】

文档声明:
以下资料均属于本人在学习过程中产出的学习笔记,如果错误或者遗漏之处,请多多指正。并且该文档在后期会随着学习的深入不断补充完善。


资料仅供学习交流使用。
作者:Aliven888

1、简述

英文为SDK的官网描述,中文为本人翻译,如果不合理之处,请参照英文原描述观看:

queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other. 【队列是一种容器适配器,专门被设计用于FIFO的场景(先入先出),元素从容器的一端插入,并且从另外一端取出。】

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the “back” of the specific container and popped from its “front”.【队列作为容器是适配器被实现,它们是使用特定容器类的封装对象作为其基础容器的类,提供了一组特定的成员函数来访问其元素。元素容器的“尾部”插入队列,然后从其“前部”取出。】

2、接口函数

名称描述
(constructor)Construct queue (public member function )
emptyTest whether container is empty (public member function )
sizeReturn size (public member function )
frontAccess next element (public member function )
backAccess last element (public member function )
pushInsert element (public member function )
emplaceConstruct and insert element (public member function )
popRemove next element (public member function )
swapSwap contents (public member function )

3、接口函数使用演示

定义一个变量

	std::queue<int> m_queueValue;

判断队列是否为空

功 能:判断队列是否为空,为空返回true, 否则返回false

	if (!m_queueValue.empty())
	{
		qDebug("m_queueValue is not empty.");
	}

插入元素

功 能:元素入队,从地=低地址到高地址依次是 [1, 2, 3, 4]

	m_queueValue.push(1);
	m_queueValue.push(2);
	m_queueValue.push(3);
	m_queueValue.push(4);

获取队列元素个数

功 能:获取当前队列的元素个数。

	int iSize = m_queueValue.size();
	qDebug("m_queueValue iSize is [%d]", iSize);

队首元素出队

功 能:元素出队,从队首(低地址)位置处出队,此时出队的元素是 1

	int iElemet = m_queueValue.front();
	qDebug("iElemet is [%d]", iElemet);

移除队首元素

功 能:移除此时位于队首(低地址)位置处的元素,此时该元素是 1

	m_queueValue.pop();

队尾元素出队

功 能:获取队位于队尾(高地址)位置的元素 此时该元素是 4

	iElemet = m_queueValue.back();
	qDebug("iElemet is [%d]", iElemet);

队列插入元素

C++ 11 引入的新特性
功 能:该函数的功能和 push() 函数类似,都是向队列中添加元素;此时队列的元素依次是 [2, 3, 4, 5]

	m_queueValue.emplace(5);

队列元素互换

C++ 11 引入的新特性
功 能:交换两个元素类型相同的队列中的元素,size随交换后的参数个数而变化;此时队列的元素依次是 m_queueValue = [6, 7, 8] queueChild = [2, 3, 4, 5]

	std::queue<int> queueChild;
	queueChild.push(6);
	queueChild.push(7);
	queueChild.push(8);
	m_queueValue.swap(queueChild);

4、注意事项

1、队列中的元素是可以重复的。
2、队列的特点是先入队的元素先出队(FIFO)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值