C++容器queue

C++容器queue

定义

  • 一种遵循先进先出(FIFO)原则的容器适配器。它基于其他序列容器实现,默认情况下使用deque作为底层容器
  • 队尾进,队首出

构造函数

1、默认构造函数

cpp 复制代码
int main()
{
	system("chcp 65001");
	queue<std::u8string> queue1;
	queue1.push(u8"北京");
	queue1.push(u8"上海");

	while (!queue1.empty())
	{
		// 返回队首数据
		cout << "queue1-> " << getStringFromU8string(queue1.front()) << endl;
		// 移除队首数据
		queue1.pop();
	}
}

2、拷贝构造函数

cpp 复制代码
int main()
{
	system("chcp 65001");
	queue<std::u8string> queue1;
	queue1.push(u8"北京");
	queue1.push(u8"上海");

	// 拷贝构造函数
	queue<std::u8string> queue2(queue1);
	while (!queue2.empty())
	{
		cout << "queue2-> " << getStringFromU8string(queue2.front()) << endl;
		queue2.pop();
	}

	while (!queue1.empty())
	{
		cout << "queue1-> " << getStringFromU8string(queue1.front()) << endl;
		queue1.pop();
	}
}

3、移动构造函数

被移动后,原队列数据清空

cpp 复制代码
int main()
{
	system("chcp 65001");
	queue<std::u8string> queue1;
	queue1.push(u8"北京");
	queue1.push(u8"上海");

	// 移动构造函数
	queue<std::u8string> queue2(std::move(queue1));
	while (!queue2.empty())
	{
		cout << "queue2-> " << getStringFromU8string(queue2.front()) << endl;
		queue2.pop();
	}

	while (!queue1.empty())
	{
		cout << "queue1-> " << getStringFromU8string(queue1.front()) << endl;
		queue1.pop();
	}
}

4、指定容器构造函数

cpp 复制代码
int main()
{
	system("chcp 65001");
	// 指定容器
	queue<std::u8string, list<std::u8string>> queue1;
	queue1.push(u8"北京");
	queue1.push(u8"上海");

	while (!queue1.empty())
	{
		cout << "queue1-> " << getStringFromU8string(queue1.front()) << endl;
		queue1.pop();
	}
}

常用函数

函数 说明 时间复杂度
void push(const T& value); value 添加到队列的末尾(队尾)。 O(1)
emplace( Args&&... args ) 元素就地构造,即不执行复制或移动操作 O(1)
void pop(); 移除队列的第一个元素 (队首)。注意:此函数不返回被移除的元素。 O(1)
T& front(); 返回对队列第一个元素 (队首)的引用。在调用前必须确保队列非空! O(1)
const T& front() const; 返回对队首元素的常量引用(用于 const 对象)。 O(1)
T& back(); 返回对队列最后一个元素 (队尾)的引用。在调用前必须确保队列非空! O(1)
const T& back() const; 返回对队尾元素的常量引用(用于 const 对象)。 O(1)
bool empty() const; 如果队列为空,返回 true,否则返回 false O(1)
size_t size() const; 返回队列中元素的数量。 O(1)
swap( queue& other ) 交换容器适配器与 other 的内容 O(1)
cpp 复制代码
int main()
{
	system("chcp 65001");
	queue<std::u8string> queue1;
	queue1.push(u8"北京");
	queue1.push(u8"上海");

	cout << "queue1.front()-> " << getStringFromU8string(queue1.front()) << endl;
	cout << "queue1.back()-> " << getStringFromU8string(queue1.back()) << endl;
	cout << "queue1.size()-> " << queue1.size() << endl;
	cout << "queue1.empty()-> " << boolalpha << queue1.empty() << endl;

	// 入队
	queue1.emplace(u8"广州");

	queue<std::u8string> queue2;
	queue2.push(u8"北京");
	queue2.push(u8"上海");

	// 交换
	queue1.swap(queue2);
	while (!queue2.empty())
	{
		cout << "queue2-> " << getStringFromU8string(queue2.front()) << endl;
		queue2.pop();
	}

	while (!queue1.empty())
	{
		cout << "queue1-> " << getStringFromU8string(queue1.front()) << endl;
		queue1.pop();
	}
}
相关推荐
Tipriest_几秒前
旋转矩阵,齐次变换矩阵,欧拉角,四元数等相互转换的常用代码C++ Python
c++·python·矩阵
毕设源码-钟学长2 分钟前
【开题答辩全过程】以 基于PHP的家常菜谱教程网站为例,包含答辩的问题和答案
开发语言·php
消失的旧时光-19437 分钟前
用 C 实现一个简化版 MessageQueue
c语言·开发语言
小鹿学程序7 分钟前
jdk配置完之后java -version还是默认的jdk版本如何更改
java·开发语言·python
至善迎风8 分钟前
Bun:下一代 JavaScript 运行时与工具链
开发语言·javascript·ecmascript·bun
程序员-King.11 分钟前
【Qt开源项目】— ModbusScope-day 5
开发语言·qt
hz_zhangrl12 分钟前
CCF-GESP 等级考试 2025年9月认证C++六级真题解析
c++·算法·青少年编程·程序设计·gesp·2025年9月gesp·gesp c++六级
老秦包你会17 分钟前
QT第五课------QT系统相关------线程
开发语言·qt
lkbhua莱克瓦2422 分钟前
IO练习——网络爬虫(爬取数据)
java·开发语言·爬虫·io流练习·java练习
net3m3329 分钟前
雅特力单片机用串口USART_INT_TDE中断比用USART_INT_TRAC的 发送效率要高
java·开发语言·算法