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();
	}
}
相关推荐
端平入洛1 天前
delete又未完全delete
c++
端平入洛2 天前
auto有时不auto
c++
郑州光合科技余经理3 天前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
feifeigo1233 天前
matlab画图工具
开发语言·matlab
dustcell.3 天前
haproxy七层代理
java·开发语言·前端
norlan_jame3 天前
C-PHY与D-PHY差异
c语言·开发语言
哇哈哈20213 天前
信号量和信号
linux·c++
多恩Stone3 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
QQ4022054963 天前
Python+django+vue3预制菜半成品配菜平台
开发语言·python·django
遥遥江上月3 天前
Node.js + Stagehand + Python 部署
开发语言·python·node.js