c++基础 STL 第四篇(queue容器和list容器)

本文详细介绍了C++标准库中的queue容器和list容器。queue是一个先进先出(FIFO)的数据结构,适用于队列操作,如插入元素到队尾,从队头移除元素。list则是一个双向循环链表,支持快速插入和删除但不支持随机访问。文章通过实例展示了queue的构造、数据存取方法,以及list的构造、赋值、大小操作、插入删除、反转和排序等功能。同时,强调了list在空间利用和插入效率上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一 queue 容器的基本概念

概念:queue是一种先进先出(First In First Out,FIFO)的数据结构,队列容器允许从一端插入元素,从另一端移除元素。
说明

  1. 通常队头为移除数据,队尾插入数据;
  2. 队列没有迭代器的操作,因为无法遍历数据,只能在队头和队尾操作数据;

二 queue 容器常用操作

1 queue 的构造函数

queue<T> que;
queue采用模板类实现,queue对象q的默认构造形式;

queue(const queue &que);
拷贝构造

queue& operator=(const queue &que);
重载等号操作符

2 queue 的数据存取

push(elem);
向队尾插入元素

pop();
移除队头元素

back();
获取队尾元素

front();
获取队头元素:

empty();
判断堆栈是否为空

size();
返回队列的大小

实例:

#include <queue>
#include <string>
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}

	string m_Name;
	int m_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);

	//队列不提供迭代器,更不支持随机访问	
	while (!q.empty()) {
		//输出队头元素
		cout << "队头元素-- 姓名: " << q.front().m_Name 
              << " 年龄: "<< q.front().m_Age << endl;
        
		cout << "队尾元素-- 姓名: " << q.back().m_Name  
              << " 年龄: " << q.back().m_Age << endl;
        
		cout << endl;
		//弹出队头元素
		q.pop();
	}

	cout << "队列大小为:" << q.size() << endl;
}

3 总结
通常把队列画成这个样子辅助代码的push()数据时候的格式记忆队列的队头队尾插入和移除:
在这里插入图片描述

三 list 容器的基本概念

概念
list 容器就是一个双向循环链表

说明

  1. list 容器是本质是链表,地址存储不是连续空间的,所以不支持随机访问,所以迭代器不可以跳跃式访问其他空间的位置,就是你不可以任意的访问其他空间的元素;
  2. list 的迭代器支++ 和 – 的操作;不支持 +1 的操作,假如支持+1 的操作,就自然而然的支持 +2 的操作了,那这样就可以随机访问了,可是list 本质是链表,很明显这是不支持的。

优点
3. 相对于vector 容器来说,list 插入数据不会造成浪费空间;在 vector 插入数据时候,会开辟一段空间,通常比原有的数据大1.5倍左右,而list容器,你用到多少数据,就开辟多少空间。
4. 相对于 vector 容器来说,list 插入和删除元素的速度比较快,因为只需要找到数据的位置,修改指针域就行,不像vector 插入删除时候要大量移动数据;

1 list 的构造函数

函数原型功能
list<T> lstist采用采用模板类实现,对象的默认构造形式
list(beg,end)构造函数将[beg, end)区间中的元素拷贝给本身
list(n,elem)构造函数将n个elem拷贝给本身
list(const list &lst)拷贝构造函数

实例:

#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

void test01()
{	//模板方式初始化
	list<int>L1;
	//插入数据
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	printList(L1);
	//将L1中的元素从[begin,end)区间拷贝到L2
	list<int>L2(L1.begin(),L1.end());
	printList(L2);
	//拷贝构造方式初始化
	list<int>L3(L2);
	printList(L3);
	// 用 10 个 1000 初始化 L4
	list<int>L4(10, 1000);
	printList(L4);
}

2 list 的赋值和交换操作

函数原型功能
assign(beg, end)将[beg, end)区间中的数据拷贝赋值给本身
assign(n, elem)将n个elem拷贝赋值给本身。
list& operator=(const list &lst)重载等号操作符
swap(lst)将lst与本身的元素互换。

实例:

#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//赋值和交换
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);
	printList(L1);

	//赋值
	list<int>L2;
	//重载 = 号
	L2 = L1;
	printList(L2);

	list<int>L3;
	//将 L2 的元素赋值给 L1
	L3.assign(L2.begin(), L2.end());
	printList(L3);
	//把 10 个 1000 赋值给 L4;
	list<int>L4;
	L4.assign(10, 100);
	printList(L4);

}

//交换
void test02()
{

	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	list<int>L2;
	L2.assign(10, 100);

	cout << "交换前: " << endl;
	printList(L1);
	printList(L2);

	cout << endl;
	// L2 与 L1 交换 元素
	L1.swap(L2);

	cout << "交换后: " << endl;
	printList(L1);
	printList(L2);

}

3 list 的大小操作

size(); //返回容器中元素的个数

empty(); //判断容器是否为空

resize(num); //重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

​ //如果容器变短,则末尾超出容器长度的元素被删除。

resize(num, elem); //重新指定容器的长度为num,若容器变长,则以elem值填充新位置。
//如果容器变短,则末尾超出容器长度的元素被删除。

函数原型功能
size()返回容器中元素的个数
empty()判断容器是否为空
resize(num)/重新指定容器的长度为num,若容器变长,填充 0;如果容器变短,则末尾超出容器长度的元素被删除
resize(num, elem)重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

实例:

#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//大小操作
void test01()
{
	list<int>L1;
	L1.push_back(10);
	L1.push_back(20);
	L1.push_back(30);
	L1.push_back(40);

	if (L1.empty())
	{
		cout << "L1为空" << endl;
	}
	else
	{
		cout << "L1不为空" << endl;
		cout << "L1的大小为: " << L1.size() << endl;
	}

	//重新指定大小
	L1.resize(10);
	//输出结果: 10 20 30 40 0 0 0 0 0 0 
	printList(L1);
	//输出结果:10 20 
	L1.resize(2);
	printList(L1);
}

4 list 的插入和删除

函数原型功能
push_back(elem)尾插elem
pop_back()尾删
push_front(elem)头插elem
pop_front()尾删
insert(pos,elem)在迭代器 pos 位置 插入elem
insert(pos,n,elem)在迭代器pos 位置插入 n个elem
insert(pos,beg,end)在 迭代器 pos 的位置插入区间[beg,end)的元素
clear()移除容器的所有数据
erase(beg,end)删除[beg,end)区间的数据
erase(pos)删除迭代器pos位置的元素
remove(elem)删除容器中所有与elem值匹配的元素。
#include <list>

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

//插入和删除
void test01()
{
	list<int> L;
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);
	//输出结果: 300 200 100 10 20 30 
	printList(L);

	//尾删
	L.pop_back();
	//输出结果: 300 200 100 10 20 
	printList(L);

	//头删
	L.pop_front();
	//输出结果: 200 100 10 20 
	printList(L);

	//插入
	list<int>::iterator it = L.begin();
	L.insert(++it, 1000);
	//输出结果: 300 1000 200 100 10 20 
	printList(L);

	//删除
	it = L.begin();
	L.erase(++it);
	//输出结果: 300  200 100 10 20 
	printList(L);

	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	//输出结果: 300 200 100 10 20 10000 10000 10000
	printList(L);
	L.remove(10000);
	//输出结果: 300 200 100 10 20 
	printList(L);
    
    //清空
	L.clear();
	//输出结果: 
	printList(L);
}

5 list 的反转和排序

函数原型功能
reverse()反转链表
sort();链表排序

注意

  1. 排序算法是默认按升序排序;
  2. 自定义的数据类型,需要自己提供函数或者反函数作为 sort的参数,去指定排序规则;

实例:

void printList(const list<int>& L) {

	for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
		cout << *it << " ";
	}
	cout << endl;
}

bool myCompare(int val1 , int val2)
{
	return val1 > val2;
}

//反转和排序
void test01()
{
	list<int> L;
	L.push_back(90);
	L.push_back(30);
	L.push_back(20);
	L.push_back(70);
	printList(L);

	//反转容器的元素
	L.reverse();
	printList(L);

	//排序
	L.sort(); //默认的排序规则 从小到大
	printList(L);
	//提供一个函数名,改变排序算法的策略
	L.sort(myCompare); //指定规则,从大到小
	printList(L);
}
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

呋喃吖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值