C++提高编程---3.6 STL-常用容器-queue 容器【P213~P214】

C++提高编程---3.6 STL-常用容器-queue 容器【P213~P214】

    • [3.6 queue 容器](#3.6 queue 容器)
      • [3.6.1 queue 容器基本概念](#3.6.1 queue 容器基本概念)
      • [3.6.2 queue 容器常用接口](#3.6.2 queue 容器常用接口)

3.6 queue 容器

3.6.1 queue 容器基本概念

概念:Queue是一种先进先出(First In First Out,FIFO)的数据结构,它有两个出口

  • 队头只能出 pop()
  • 队尾只能进 push()

只有队头、队尾能被外界访问,因此不能有遍历行为

3.6.2 queue 容器常用接口

cpp 复制代码
#include<iostream>
#include<queue>
#include<string>


using namespace std;

// 队列 queue

class Person {
public:
	Person(string name, int age) {
		this->name_ = name;
		this->age_ = age;
	}
		
	string name_;
	int age_;
};

void test01() {
	queue<Person> q;
	 
	// 准备数据
	Person p1("唐僧", 30);
	Person p2("孙悟空", 1000);
	Person p3("猪八戒", 900);
	Person p4("沙僧", 800);

	// 如队
	q.push(p1);
	q.push(p2);
	q.push(p3);
	q.push(p4);

	cout << "当前队列长度为:" << q.size() << endl;


	// 判断队列不为空,查看队头队尾,出队
	while (!q.empty()) {
		cout << "队头为:姓名------" << q.front().name_ << "年龄------" << q.front().age_ << endl;
		cout << "队尾为:姓名------" << q.back().name_ << "年龄------" << q.back().age_ << endl;
		// 出队
		q.pop();
		// 查看队列大小
		cout << "当前队列长度为:" << q.size() << endl;
	}

}

int main() {
	
	test01();

	system("pause");
	return 0;
}
相关推荐
clint4561 天前
C++进阶(1)——前景提要
c++
夜悊1 天前
C++代码示例:进制数简单生成工具
c++
郝学胜_神的一滴2 天前
CMake 021: IF 条件判据详诠
c++·cmake
_wyt0012 天前
洛谷 B3930 [GESP202312 五级] 烹饪问题 题解
c++·gesp
玖玥拾2 天前
C/C++ 数据结构(七)栈、容器适配器
c语言·数据结构·c++··容器适配器
один but you2 天前
constexpr函数
c++
凡人叶枫2 天前
Effective C++ 条款41:了解隐式接口和编译期多态
java·开发语言·c++·effective c++
凡人叶枫2 天前
Effective C++ 条款42:了解 typename 的双重意义
java·linux·服务器·c++
小胖xiaopangss2 天前
BRpc使用
c++·rpc
-森屿安年-2 天前
63. 不同路径 II
c++·算法·动态规划