[STL]list使用介绍

[STL]list使用

注:本文测试环境是visual studio2019。

文章目录

  • [STL]list使用
    • 1. list介绍
    • 2. 构造函数
    • 3. 迭代器相关函数
      • begin函数和end函数
      • rbegin函数和rend函数
    • 4. 容量相关函数
      • empty函数
      • size函数
    • 5. 数据修改函数
      • push_back函数和pop_back函数
      • push_front函数和pop_front函数
      • insert函数和erase函数
      • swap函数
      • resize函数
      • clear函数
    • 6. 数据操作函数
      • sort函数
      • reverse函数
      • merge函数
      • unique函数
      • remove函数
      • splice函数

1. list介绍

  1. list是可以在常量时间内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
  2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
  3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置。

2. 构造函数

image-20230618212401437

(1)构造函数

默认构造函数。

list<int> l; //创建一个空list

(2)构造函数

创建一个有n个结点,结点数据为val的list。

list<int> l(10, 66); 

(3)构造函数

使用迭代器构造列表。

vector<int> v(10, 6);
list<int> l(v.begin(), v.end()); //创建一个存储int类型的链表,使用v初始化数据

(4)构造函数

拷贝构造函数

list<int> l1(10,6);
list<int> l2(l1); //使用l2拷贝构造l1

3. 迭代器相关函数

begin函数和end函数

begin函数:

返回指向list第一个结点的正向迭代器。

end函数:

返回指向list结尾的正向迭代器。

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl; //输出为 1 2 3 4
	return 0;
}

rbegin函数和rend函数

rbegin函数:

返回指向list最后一个含有数据的结点的反向迭代器。

rend函数:

指向list反向结尾的反向迭代器。

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	list<int>::reverse_iterator it = l.rbegin();
	while (it != l.rend())
	{
		cout << *it << " ";
		it++;
	}
	cout << endl; //输出为 4 3 2 1
	return 0;
}

4. 容量相关函数

empty函数

判断list是否为空.

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	list<int> l1;
	list<int> l2(5, 6);
	cout << l1.empty() << endl;//输出为1
    cout << l2.empty() << endl;//输出为0
	return 0;
}

size函数

获取list存储的结点个数。

#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{
	vector<int> v;
	v.push_back(1); //在list中尾插数据1
	v.push_back(2); //在list中尾插数据2
	v.push_back(3);	//在list中尾插数据3
	v.push_back(4);	//在list中尾插数据4
	list<int> l(v.begin(), v.end());
	cout << l.size() << endl;  //输出为4
	return 0;
}

5. 数据修改函数

push_back函数和pop_back函数

push_back函数:

在list结尾插入结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	cout << l.size() << endl; //输出为3
	return 0;
}

pop_back函数:

在list结尾删除结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	cout << l.size() << endl; //输出为3
	l.pop_back();
	l.pop_back();
	cout << l.size() << endl; //输出为1
	l.pop_back();
	//l.pop_back(); -- 报错 -- 没有结点可删
	return 0;
}

push_front函数和pop_front函数

push_front函数:

在list中头插结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 4 3 2 1
	return 0;
}

pop_front函数:

在list中头删结点。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_front(1);
	l.push_front(2);
	l.push_front(3);
	l.push_front(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;
	l.pop_front();
	l.pop_front();
	it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
    cout << endl; // 输出为 2 1
	return 0;
}

insert函数和erase函数

和vector容器类似,list容器也没有提供find函数,而insert函数和erase函数是需要配合迭代器使用的,因此需要使用算法库的find函数。

image-20230619211015257

find函数查找成功会返回指向数据的迭代器,失败会返回传入的last迭代器,注意find函数的查找范围是从first迭代器至last迭代器前,不包括last迭代器。

insert函数

功能1: 在某一位置插入结点。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.insert(pos, 66);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; //输出为1 66 2 3 4
	return 0;
}

功能2: 在某一位置插入n个存储相同数据的结点。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.insert(pos, 3, 66);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; //输出为1 66 66 66 2 3 4
	return 0;
}

功能3: 传入其他list容器或者其他类型容器的迭代器,将传入的迭代器区间内的数据插入。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l1(3, 66);
	list<int> l2;
	l2.push_back(1);
	l2.push_back(2);
	l2.push_back(3);
	list<int>::iterator pos = find(l2.begin(), l2.end(), 2);
	l2.insert(pos, l1.begin(), l1.end());
	list<int>::iterator it = l2.begin();
	while (it != l2.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl;  //输出为1 66 66 66 2 3
	return 0;
}

erase函数

功能1: 删除迭代器指向的结点。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	list<int>::iterator pos = find(l.begin(), l.end(), 2);
	l.erase(pos);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 1 3
	return 0;
}

功能2: 将迭代器范围的结点都删除。

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	list<int>::iterator start = find(l.begin(), l.end(), 2);
	list<int>::iterator finish = find(l.begin(), l.end(), 5);
	l.erase(start, finish);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << " ";
		++it;
	}
	cout << endl; // 输出为 1 5
	return 0;
}

swap函数

将两个list数据交换,通过交换list指向的头结点实现。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1(3, 66);
	list<int> l2;
	l2.push_back(1);
	l2.push_back(2);
	l2.push_back(3);
	l1.swap(l2);
	list<int>::iterator it1 = l1.begin();
	while (it1 != l1.end())
	{
		cout << *it1 << " ";  //输出为 1 2 3
		++it1;
	}
	cout << endl;
	list<int>::iterator it2 = l2.begin();
	while (it2 != l2.end())
	{
		cout << *it2 << " "; //输出为 66 66 66
		++it2;
	}
	cout << endl;
	return 0;
}

resize函数

image-20230619215305106

  1. 如果n < size, 会将结点删除至list只有n个结点,由于list结点的释放成本是不高的,因此不同于vector只是将末尾指向修改,list会实际上的在调用resize函数时的删除结点。
  2. 如果n > size,就将插入结点使得list有n个结点
#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	cout << l.size() << endl; //输出为5
	l.resize(3); // n < size
	cout << l.size() << endl; //输出为3
	l.resize(6); // n > size
	cout << l.size() << endl; //输出为6
	return 0;
}

clear函数

释放所有结点,使得list为空链表。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l(66, 6);
	cout << l.size() << endl; // 输出为66
	l.clear();
	cout << l.size() << endl; // 输出为0
	return 0;
}

6. 数据操作函数

sort函数

对list中的数据进行排序。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1;
	l1.push_back(2);
	l1.push_back(1);
	l1.push_back(4);
	l1.push_back(7);
	l1.push_back(5);
	l1.push_back(9);
	l1.push_back(8);

	l1.sort();
	list<int>::iterator it = l1.begin();
	it = l1.begin();
	while (it != l1.end())
	{
		cout << *it << ' ';  //输出为: 1 2 4 5 7 8 9
		it++;
	}

	return 0;
}

注: 由于list是链表实现的,迭代器是双向迭代器,因此不能调用algorithm库内的sort函数,因此需要单独设计sort函数,但是list的sort函数由于结构原因导致性能不高。

reverse函数

将list内的结点逆置。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';  //输出为: 1 2 3 4
		it++;
	}
	l.reverse(); //将list逆置
	it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' '; //输出为: 4 3 2 1
		it++;
	}
    cout << endl;
	return 0;
}

merge函数

如果两个list有序并且排序方式相同,可以将一个list的结点连接到另一个list上并保持有序,排序方式和连接前相同。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);
    
	list<int> l2;
	l2.push_back(6);
	l2.push_back(7);
	l2.push_back(8);
	l2.push_back(9);
    
	l2.merge(l1); //将l1的结点连接到l2,连接后l1为空
    
	list<int>::iterator it = l2.begin();
	while (it != l2.end())
	{
		cout << *it << ' ';
		it++;
	}
    cout << endl;
	return 0;
}

unique函数

只能给数据有序的list进行数据去重操作,如果数据无序去重操作会出现问题。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
    l.push_back(2);
    l.push_back(2);
	l.push_back(3);
	l.push_back(3);
	l.push_back(3);
	l.push_back(4);
	l.unique();	//对list内的数据去重
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' ';	// 输出为: 1 2 3 4
		it++;
	}
    cout << endl;
	return 0;
}

remove函数

查找对应的数据并进行删除操作,数据存在就删除,不存在不会进行任何操作。

#include <iostream>
#include <list>
using namespace std;
int main()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);

	l.remove(3); //删除数据为3的结点
	list<int>::iterator it = l.begin();
	while (it != l.end())
	{
		cout << *it << ' '; //输出为: 1 2 4
		it++;
	}
    cout << endl;
	return 0;
}

splice函数

将以一个list的指定部分的结点转移给其他list。

#include <iostream>
#include <list>
using namespace std;

int main()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(2);
	l1.push_back(3);
	l1.push_back(4);

	list<int> l2;
	l2.push_back(5);
	l2.push_back(6);
	l2.push_back(9);
	l2.push_back(8);
	
	list<int>::iterator it = l1.begin();
	l1.splice(it, l2); //将l2的所有结点转移到l1的begin位置
	it = l1.begin();
	while (it != l1.end())
	{
		cout << *it << ' '; //输出为:5 6 9 8 1 2 3 4
		it++;
	}

	return 0;
}

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

位图和布隆过滤器+哈希切分思想

文章目录 一.位图(bitset)底层实现: 二.布隆过滤器(bloomFilter)底层实现: 三.哈希切分思想 一.位图(bitset) 位图是一种以一个比特位为数据记录单元的哈希表 ,以无符号整数为key值,采用直接定址法(不存在哈希冲突的问题),其哈希映射函数为 f ( k e y ) k e y ( k e y 的存在…

快速排序qsort讲解

hello大家好&#xff0c;我是c语言boom家宝&#xff0c;今天为大家分享的博客内容是qsort快速排序&#xff0c;简称快排的一个知识点的讲解。 在讲到快排之前&#xff0c;允许博主先提一嘴冒泡排序。大家在c语言的学习过程中&#xff0c;冒泡排序是必不可少会学习到的一个思想&…

Hudi数据湖技术引领大数据新风口(三)解决spark模块依赖冲突

文章目录 解决spark模块依赖冲突2.2.6 执行编译命令2.2.7 编译成功 下一章 核心概念后记 解决spark模块依赖冲突 修改了Hive版本为3.1.2&#xff0c;其携带的jetty是0.9.3&#xff0c;hudi本身用的0.9.4&#xff0c;存在依赖冲突。 1&#xff09;修改hudi-spark-bundle的pom文…

kafka集群

目录 broker ZooKeeper consumer group&#xff08;消费者组&#xff09; 分区&#xff08;Partitions&#xff09; 副本&#xff08;Replicas&#xff09; 主题&#xff08;Topic&#xff09; 偏移量&#xff08;offset&#xff09; broker 一个kafka进程就是一个broker…

备战秋招 | 笔试强训17

目录 一、选择题 二、编程题 三、选择题题解 四、编程题题解 一、选择题 1、假设A为抽象类&#xff0c;下列声明&#xff08;&#xff09;是正确的 A. int fun(A); B. A Obj; C. A fun(int); D. A *p; 2、虚函数可不可以重载为内联&#xff1f; A. 可以 B. 不可以 C. 语法…

uni-app踩坑记

打包h5如何配置域名&#xff1a; 在manifest.json中配置域名 配置完成后无论是测试环境还是正式环境都带上/mobile/&#xff0c;否则会报错404 如何引入调试工具erada: 在默认的index.html中直接引入erada&#xff0c;页面样式会整个错乱&#xff0c;解决方案就是引入官方…

低代码开发平台源码

什么是低代码开发平台&#xff1f; 低代码来源于英文“Low Code&#xff0c;它意指一种快速开发的方式&#xff0c;使用最少的代码、以最快的速度来交付应用程序。通俗的来说&#xff0c;就是所需代码数量低&#xff0c;开发人员门槛低&#xff0c;操作难度低。一般采用简单的图…

学习笔记|大模型优质Prompt开发与应用课(二)|第二节:超高产文本生成机,传媒营销人必备神器

文章目录 01 文字写作技能的革新&#xff0c;各行各业新机遇四大类常见文字工作新闻记者的一天新闻记者的一天–写策划prompt 新闻记者的一天–排采访prompt生成结果prompt生成结果 大模型加持&#xff0c;文字写作我们如何提效营销创作营销创作-使用预置法为不同平台生成文案p…

产品开发八大模块交流︱奇瑞新能源汽车产品开发院院长荣升格

奇瑞新能源汽车股份有限公司研发中心/产品开发院院长荣升格先生受邀为由PMO评论主办的2023第十二届中国PMO大会演讲嘉宾&#xff0c;演讲议题&#xff1a;产品开发八大模块交流。大会将于8月12-13日在北京举办&#xff0c;敬请关注&#xff01; 议题简要&#xff1a; VUCA时代…

从源码分析Handler面试问题

Handler 老生常谈的问题了&#xff0c;非常建议看一下Handler 的源码。刚入行的时候&#xff0c;大佬们就说 阅读源码 是进步很快的方式。 Handler的基本原理 Handler 的 重要组成部分 Message 消息MessageQueue 消息队列Lopper 负责处理MessageQueue中的消息 消息是如何添加…

用WhatsApp开拓和跟进客户,需要注意这些雷点

我们很多新手小白在利用WhatsApp开拓和维护客户的时候&#xff0c;总是容易犯一些错误&#xff0c;踩到雷点&#xff0c;这不利于客户对企业的印象&#xff0c;不利于增长&#xff0c;下面我们来说一些需要注意的点&#xff1a; 1、专业正确的用语 不管外贸人是跟进哪个国家…

29.Git版本控制工具

1.Git简介 Git是一开源的分布式版本控制系统&#xff0c;提供了存储代码、管理版本历史、分支和合并等功能。 版本控制是指对软件开发过程中各种程序代码、配置文件及说明文档等文件变更的管理&#xff0c;是软件配置管理的核心思想之一。它的主要目的是跟踪和记录软件开发过程…

双重for循环优化

项目中有段代码逻辑是个双重for循环&#xff0c;发现数据量大的时候&#xff0c;直接导致数据接口响应超时&#xff0c;这里记录下不断优化的过程&#xff0c;算是抛砖引玉吧~ Talk is cheap,show me your code&#xff01; 双重for循环优化 1、数据准备2、原始双重for循环3、…

如何利用Requestly提升前端开发与测试的效率

痛点 B站最牛的Python接口自动化测试进阶教程合集&#xff08;真实企业项目实战&#xff09; 前端测试 在进行前端页面开发或者测试的时候&#xff0c;我们会遇到这一类场景&#xff1a; 在开发阶段&#xff0c;前端想通过调用真实的接口返回响应在开发或者生产阶段需要验证前…

Vue 中通用的 css 列表入场动画效果

css 代码 .gradientAnimation {animation-name: gradient;animation-duration: 0.85s;animation-fill-mode: forwards;opacity: 0; }/* 不带前缀的放到最后 */ keyframes gradient {0% {opacity: 0;transform: translate(-100px, 0px);}100% {opacity: 1;transform: translate…

算法38:反转链表

一、需求 给你单链表的头节点 head &#xff0c;请你反转链表&#xff0c;并返回反转后的链表。 示例 1&#xff1a; 输入&#xff1a;head [1,2,3,4,5] 输出&#xff1a;[5,4,3,2,1] 示例 2&#xff1a; 输入&#xff1a;head [1,2] 输出&#xff1a;[2,1] 示例3&#xff…

MSFCS互相联动

MSF&CS互相联动 1. 前言2. CS联动MSF2.1. 案例测试2.1.1. CS设置联动监听器2.1.2. CS设置联动MSF会话2.1.3. MSF设置监听 3. MSF联动CS3.1. 案例测试3.1.1. MSF生成木马3.1.2. 设置监听3.1.3. CS设置监听3.1.4. MSF转移会话3.1.5. 查看上线 4. 其它 1. 前言 在日常渗透测试…

基于K8s环境·使用ArgoCD部署Jenkins和静态Agent节点

今天是「DevOps云学堂」与你共同进步的第 47天 第⑦期DevOps实战训练营 7月15日已开营 实践环境升级基于K8s和ArgoCD 本文节选自第⑦期DevOps训练营 &#xff0c; 对于训练营的同学实践此文档依赖于基础环境配置文档&#xff0c; 运行K8s集群并配置NFS存储。实际上只要有个K8s集…

VMPWN的入门级别题目详解(二)

实验四 VMPWN4 题目简介 这道题应该算是虚拟机保护的一个变种&#xff0c;是一个解释器类型的程序&#xff0c;何为解释器&#xff1f;解释器是一种计算机程序&#xff0c;用于解释和执行源代码。解释器可以理解源代码中的语法和语义&#xff0c;并将其转换为计算机可以执行的…

史上最强,Python自动化测试框架整理,搭建框架看这篇就够了...

目录&#xff1a;导读 前言一、Python编程入门到精通二、接口自动化项目实战三、Web自动化项目实战四、App自动化项目实战五、一线大厂简历六、测试开发DevOps体系七、常用自动化测试工具八、JMeter性能测试九、总结&#xff08;尾部小惊喜&#xff09; 前言 什么是测试框架呢…
最新文章