list从0到1的突破

news2024/9/21 19:52:51

目录

前言

1.list的介绍

2.list的常见接口

2.1 构造函数( (constructor))  +接口说明    

2.2 list iterator 的使用

 2.3 list capacity

2.4 list element access

2.5 list modifiers

3.list的迭代器失效

附整套练习源码

结束语


前言

前面我们学习了vector,本节我们将对新的容器list进行拆分学习,并且有了string和vector的基础,list容器的方法学习起来就会轻松很多。

1.list的介绍

 1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高 效。

4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。

5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这可能是一个重要的因素)

2.list的常见接口

2.1 构造函数( (constructor))  +接口说明    

list (size_type n, const value_type& val = value_type())     构造的list中包含n个值为val的元素

list()         拷贝空的list

list (const list& x)     拷贝构造函数

list (InputIterator first, InputIterator last)      用[first, last)区间中的元素构造list

void test1() {
	list<int>l1;
	list<int>l2(5, 10);
	list<int>l3(l2.begin(), l2.end());//迭代器构造
	list<int>l4(l2);//拷贝构造
	//以数组区间迭代器构造list
	float arr[] = { 5.20,13.14,9.99,8.88 };
	list<float>l5(arr, arr + sizeof(arr) / sizeof(float));
	// 列表格式初始化C++11
	list<int> l6{ 1,2,3,4,5 };

	// 用迭代器方式打印l5中的元素
	list<float> ::iterator it = l5.begin();
	while (it != l5.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
	// C++11范围for的方式遍历
	for (auto e : l6) {
		cout << e << " ";
	}
}

注意:遍历链表只能用迭代器和范围for

2.2 list iterator 的使用

void TestList2()
{
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    list<int> l(array, array + sizeof(array) / sizeof(array[0]));
    // 使用正向迭代器正向list中的元素
    // list<int>::iterator it = l.begin();   // C++98中语法
    auto it = l.begin();                     // C++11之后推荐写法
    while (it != l.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    // 使用反向迭代器逆向打印list中的元素
    // list<int>::reverse_iterator rit = l.rbegin();
    auto rit = l.rbegin();
    while (rit != l.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;
}

跟vector几乎是一样的

注意:

1. begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动

2. rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动 

 2.3 list capacity

2.4 list element access

 官方测试代码展示

list<int> mylist;

mylist.push_back(10);

while (mylist.back() != 0)
{
	mylist.push_back(mylist.back() - 1);
}

cout << "mylist contains:";
for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
	std::cout << ' ' << *it;

cout << '\n';

 

2.5 list modifiers

push_front    在list首元素前插入值为val的元素       pop_front    删除list中第一个元素 

push_back     在list尾部插入值为val的元素             pop_back删除list中最后一个元素 insert    在list position 位置中插入值为val的元素   erase删除list position位置的元素

swap交换两个list中的元素                                        clear清空list中的有效元素 

void print_list(const list<int>& ml) {
	// 注意这里调用的是list的 begin() const,返回list的const_iterator对象
	list<int>::const_iterator it = ml.begin();
	while (it != ml.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
void TestList3() {
	list<int>mylist{ 1,2,3,4,5 };
	mylist.push_back(6);
	mylist.push_front(0);
	print_list(mylist);
	mylist.pop_back();
	mylist.pop_front();
	print_list(mylist);
}
void TestList4()
{
	int array1[] = { 1, 2, 3 };
	list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));

	// 获取链表中第二个节点
	//auto pos = ++L.begin();
	list<int>::iterator pos = ++L.begin();
	cout << *pos << endl;

	// 在pos前插入值为4的元素
	L.insert(pos, 4);
	print_list(L);

	// 在pos前插入5个值为5的元素
	L.insert(pos, 5, 5);
	print_list(L);

	// 在pos前插入[v.begin(), v.end)区间中的元素
	vector<int> v{ 7, 8, 9 };
	L.insert(pos, v.begin(), v.end());
	print_list(L);

	// 删除pos位置上的元素
	L.erase(pos);
	print_list(L);

	// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
	L.erase(L.begin(), L.end());
	print_list(L);
}

 这里我们设置了一个打印链表值的函数,方便打印链表,只是打印整数,想打印其他值可以参考vector建立一个模版打印函数,让编译器自己推测打印数据的类型。

template <class Container>
void print(const Container& v) {
	auto it = v.begin();
	while (it != v.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
void TestList5()
{
	// 用数组来构造list
	int array1[] = { 1, 2, 3 ,4 ,5};
	list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
	print_list(l1);
	list<int>l2{ 6,7,8,9,10 };
	// 交换l1和l2中的元素
	l1.swap(l2);
	print_list(l1);
	print_list(l2);

	// 将l2中的元素清空
	l2.clear();
	cout << l2.size() << endl;
}

3.list的迭代器失效

此处可将迭代器暂时理解成类似于指针,迭代器失效即迭代器所指向的节点的无效,即该节 点被删除了。因为list的底层结构为带头结点的双向循环链表,因此在list中进行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,并且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。

void TestList() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	list<int>l1 (arr, arr + sizeof(arr) / sizeof(arr[0]));
	auto it = l1.begin();
	while (it != l1.end()) {
		l1.erase(it);
		it++;
	}
}

修改后:

void TestList() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	list<int>l1 (arr, arr + sizeof(arr) / sizeof(arr[0]));
	print(l1);
	auto it = l1.begin();
	while (it != l1.end()) {
		//等价于l1.erase(it++);
		it = l1.erase(it);
		
	}
	print(l1);
}

 变式删除偶数:

void TestList1() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };

	list<int>l1(arr, arr + sizeof(arr) / sizeof(arr[0]));
	print(l1);
	auto it = l1.begin();
	while (it != l1.end()) {
		if(*it%2==0)
		it = l1.erase(it);
		else
		it++;
	}
	print(l1);
}

附整套练习源码

#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <vector>
using namespace std;
void test1() {
	list<int>l1;
	list<int>l2(5, 10);
	list<int>l3(l2.begin(), l2.end());//迭代器构造
	list<int>l4(l2);//拷贝构造
	//以数组区间迭代器构造list
	float arr[] = { 5.20,13.14,9.99,8.88 };
	list<float>l5(arr, arr + sizeof(arr) / sizeof(float));
	// 列表格式初始化C++11
	list<int> l6{ 1,2,3,4,5 };

	// 用迭代器方式打印l5中的元素
	list<float> ::iterator it = l5.begin();
	while (it != l5.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
	// C++11范围for的方式遍历
	for (auto e : l6) {
		cout << e << " ";
	}
	cout << endl;
	cout << l5.size() << endl;
}
void TestList2()
{
	int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
	list<int> l(array, array + sizeof(array) / sizeof(array[0]));
	// 使用正向迭代器正向list中的元素
	// list<int>::iterator it = l.begin();   // C++98中语法
	auto it = l.begin();                     // C++11之后推荐写法
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;

	// 使用反向迭代器逆向打印list中的元素
	// list<int>::reverse_iterator rit = l.rbegin();
	auto rit = l.rbegin();
	while (rit != l.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
}
void test3() {

		list<int> mylist;

		mylist.push_back(10);

		while (mylist.back() != 0)
		{
			mylist.push_back(mylist.back() - 1);
		}

		cout << "mylist contains:";
		for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
			std::cout << ' ' << *it;

		cout << '\n';

}
template <class Container>
void print(const Container& v) {
	auto it = v.begin();
	while (it != v.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
void print_list(const list<int>& ml) {
	// 注意这里调用的是list的 begin() const,返回list的const_iterator对象
	list<int>::const_iterator it = ml.begin();
	while (it != ml.end()) {
		cout << *it << " ";
		it++;
	}
	cout << endl;
}
void TestList3() {
	list<int>mylist{ 1,2,3,4,5 };
	mylist.push_back(6);
	mylist.push_front(0);
	print_list(mylist);
	mylist.pop_back();
	mylist.pop_front();
	print_list(mylist);
}
void TestList4()
{
	int array1[] = { 1, 2, 3 };
	list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));

	// 获取链表中第二个节点
	//auto pos = ++L.begin();
	list<int>::iterator pos = ++L.begin();
	cout << *pos << endl;

	// 在pos前插入值为4的元素
	L.insert(pos, 4);
	print_list(L);

	// 在pos前插入5个值为5的元素
	L.insert(pos, 5, 5);
	print_list(L);

	// 在pos前插入[v.begin(), v.end)区间中的元素
	vector<int> v{ 7, 8, 9 };
	L.insert(pos, v.begin(), v.end());
	print_list(L);

	// 删除pos位置上的元素
	L.erase(pos);
	print_list(L);

	// 删除list中[begin, end)区间中的元素,即删除list中的所有元素
	L.erase(L.begin(), L.end());
	print_list(L);
}
void TestList5()
{
	// 用数组来构造list
	int array1[] = { 1, 2, 3 ,4 ,5};
	list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
	//print_list(l1);
	print(l1);
	list<int>l2{ 6,7,8,9,10 };
	// 交换l1和l2中的元素
	l1.swap(l2);
	//print_list(l1);
	//print_list(l2);
	print(l1);
	print(l2);

	// 将l2中的元素清空
	l2.clear();
	cout << l2.size() << endl;
}
void TestList() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };
	list<int>l1 (arr, arr + sizeof(arr) / sizeof(arr[0]));
	print(l1);
	auto it = l1.begin();
	while (it != l1.end()) {
		//等价于l1.erase(it++);
		it = l1.erase(it);
		
	}
	print(l1);
}
void TestList1() {
	int arr[] = { 1,2,3,4,5,6,7,8,9 };

	list<int>l1(arr, arr + sizeof(arr) / sizeof(arr[0]));
	print(l1);
	auto it = l1.begin();
	while (it != l1.end()) {
		if(*it%2==0)
		it = l1.erase(it);
		else
		it++;
	}
	print(l1);
}
int main() {
	//test3();
	TestList1();
	return 0;
}

结束语

本节内容就到此结束啦,相信大家对list有了进一步的了解,下节我们将一步一步实现自己的list!

最后感谢各位友友的支持,给小编点个赞吧!!! 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2142697.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Defining Constraints with ObjectProperties

步骤4&#xff1a;使用对象定义约束 物业 您可以创建时间和放置约束&#xff0c;如本教程所示。你也可以 更改单元格的属性以控制Vivado实现如何处理它们。许多 物理约束被定义为单元对象的属性。 例如&#xff0c;如果您在设计中发现RAM存在时序问题&#xff0c;为了避免重新合…

C语言代码练习(第二十六天)

今日练习&#xff1a; 数据的交换输出输入 n 个数&#xff0c;找出其中最小的数&#xff0c;将它与最前面的数交换后输出这些数 输入一个英文句子&#xff0c;将每个单词的第一个字母改成大写字母 输入一个十进制数 N &#xff0c;将它转换成 R 进制数输出 数据的交换输出输入 …

阿里OSS对象存储服务,实现图片上传回显

阿里OSS对象存储服务 OSS服务1. 创建buckte2. 获取accesskey3. 参照官方SDK编写程序安装SDK 4. 程序编写5. 封装6. 在spring中调用 OSS服务 阿里云对象存储 OSS&#xff08;Object Storage Service&#xff09;是一款海量、安全、低成本、高可靠的云存储服务&#xff0c;提供最…

利用JS数组根据数据生成柱形图

要求 <html> <head><meta charset"UTF-8"><meta http-equiv"X-UA-Compatible" content"IEedge"><meta name"viewport" content"widthdevice-width, initial-scale1.0"><title>Document…

精准识别,高效管理:工服识别AI检测算法在多场景中的应用优势

随着人工智能技术的快速发展&#xff0c;其在各个行业的应用也日益广泛。特别是在工业生产和安全监管领域&#xff0c;工服识别AI检测算法凭借其高效、精准的特点&#xff0c;成为提升生产效率、保障工作人员安全的重要手段。本文将详细介绍TSINGSEE青犀AI智能分析网关V4工服识…

Hibernate基础

Hibernate基础总结 有利的条件和主动的恢复产生于再坚持一下的努力之中&#xff01; 好久没更新了&#xff0c;今天入门了Hibernate&#xff0c;由于之前学习了MyBatis&#xff0c;初步感觉二者的底层实现思想有很多相似之处&#xff0c;下面让我们以一个入门Demo的形式感受一…

3.Java高级编程实用类介绍(一)

三、Java高级编程实用类介绍(一) 文章目录 三、Java高级编程实用类介绍(一)一、枚举类型二、包装类三、Math 一、枚举类型 使用enum进行定义 public enum 枚举名字{值1,值2.... }二、包装类 每个基本类型在java.lang包中都有一个相应的包装类 /** new包装类&#xff08;字符…

【C++笔记】类和对象的深入理解(三)

【C笔记】类和对象的深入理解(三) &#x1f525;个人主页&#xff1a;大白的编程日记 &#x1f525;专栏&#xff1a;C笔记 文章目录 【C笔记】类和对象的深入理解(三)前言一.日期类的实现1.1声明和定义分离1.2日期类整数1.3日期类整数1.4日期类-整数1.5日期类-日期1.6复用对…

并发安全与锁

总述 这篇文章&#xff0c;我想谈一谈自己对于并发变成的理解与学习。主要涉及以下三个部分&#xff1a;goroutine&#xff0c;channel以及lock 临界区 首先&#xff0c;要明确下面两组概念 并发和并行 并行&#xff1a;指几个程序每时每刻都同时进行 并发&#xff1a;指…

lnmp - 登录技术方案设计与实现

概述 登录功能是对于每个动态系统来说都是非常基础的功能&#xff0c;用以区别用户身份、和对应的权限和信息&#xff0c;设计出一套安全的登录方案尤为重要&#xff0c;接下来我介绍一下常见的认证机制的登录设计方案。 方案设计 HTTP 是一种无状态的协议&#xff0c;客户端…

iOS - TestFlight使用

做的项目需要给外部人员演示&#xff0c;但是不方便获取对方设备的UDID&#xff0c;于是采用TestFlight 的方式邀请外部测试人员的方式给对方安装测试App&#xff0c;如果方便获取对方设备的UDID&#xff0c;可以使用蒲公英 1.在Xcode中Archive完成后上传App Store Connect之前…

浙大上交联合阿里腾讯,共同构建医学AI领域的顶尖科研+商业团队|个人观点·24-09-17

小罗碎碎念 昨晚锻炼时&#xff0c;我想着是时候对推文的内容做一些改进了——既能通过写推文来锻炼自己写paper的能力&#xff0c;也希望凭借自己一点微弱的影响力&#xff0c;去带动更多的人加入医学AI的队伍中。 这一期推文系统且深度的分析一下&#xff0c;国内哪些学者在医…

Linux基础开发环境(git的使用)

1.账号注册 git 只是一个工具&#xff0c;要想实现便捷的代码管理&#xff0c;就需要借助第三方平台进行操作&#xff0c;当然第三平台也是基于git 开发的 github 与 gitee 代码托管平台有很多&#xff0c;这里我们首选 Github &#xff0c;理由很简单&#xff0c;全球开发者…

算法题之回文子串

回文子串 给你一个字符串 s &#xff0c;请你统计并返回这个字符串中 回文子串 的数目。 回文字符串 是正着读和倒过来读一样的字符串。 子字符串 是字符串中的由连续字符组成的一个序列。 示例 1&#xff1a; 输入&#xff1a;s "abc" 输出&#xff1a;3 解释…

C++ 带约束的Ceres形状拟合

C 带约束的Ceres形状拟合 一、Ceres Solver1.定义问题2. 添加残差AddResidualBlockAutoDiffCostFunction 3. 配置求解器4. 求解5. 检查结果 二、基于Ceres的最佳拟合残差结构体拟合主函数 三、带约束的Ceres拟合残差设计拟合区间限定 四、拟合结果bestminmax 五、完整代码 对Ce…

RocksDB系列一:基本概念

0 引言 RocksDB 是 Facebook 基于 Google 的 LevelDB 代码库于 2012 年创建的高性能持久化键值存储引擎。它针对 SSD 的特定特性进行了优化&#xff0c;目标是大规模&#xff08;分布式&#xff09;应用&#xff0c;并被设计为嵌入在更高层次应用中的库组件。RocksDB应用范围很…

【Python百日进阶-Web开发-音频】Day711 - 光谱表示 librosa.stft 短时傅里叶变换

文章目录 一、光谱表示 Spectral representations1.1 librosa.stft1.1.1 语法与参数1.1.2 示例 一、光谱表示 Spectral representations 1.1 librosa.stft https://librosa.org/doc/latest/generated/librosa.stft.html 1.1.1 语法与参数 librosa.stft(y, *, n_fft2048, ho…

智能机巢+无人机:自动化巡检技术详解

智能机巢与无人机的结合&#xff0c;在自动化巡检领域展现出了巨大的潜力和优势。以下是对这一技术的详细解析&#xff1a; 一、智能机巢概述 智能机巢&#xff0c;也被称为无人机机场或无人机机巢&#xff0c;是专门为无人机提供停靠、充电、维护等服务的智能化设施。它不仅…

加密与安全_优雅存储二要素(AES-256-GCM )

文章目录 什么是二要素如何保护二要素&#xff08;姓名和身份证&#xff09;加密算法分类场景选择算法选择AES - ECB 模式 (不推荐)AES - CBC 模式 (推荐)GCM&#xff08;Galois/Counter Mode&#xff09;AES-256-GCM简介AES-256-GCM工作原理安全优势 应用场景其他模式 和 敏感…

LeetcodeTop100 刷题总结(一)

LeetCode 热题 100&#xff1a;https://leetcode.cn/studyplan/top-100-liked/ 文章目录 一、哈希1. 两数之和49. 字母异位词分组128. 最长连续序列 二、双指针283. 移动零11. 盛水最多的容器15. 三数之和42. 接雨水&#xff08;待完成&#xff09; 三、滑动窗口3. 无重复字符的…