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();
	}
}
相关推荐
卷无止境6 小时前
Eigen 库如何借助 OpenMP 加速计算
c++·后端
卷无止境6 小时前
OpenMPI、MPICH 与 OpenMP:关系、核心概念与架构全解
c++·后端
郝学胜_神的一滴1 天前
CMake 30:循环语法全解|foreach_while双循环精讲、迭代技巧与实战避坑指南
c++·cmake
卷无止境3 天前
C++ 的Eigen 库全解析
c++
卷无止境3 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴3 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18005 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴5 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
众少成多积小致巨6 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
clint45610 天前
C++进阶(1)——前景提要
c++