[STL]list使用介绍

news2024/9/30 11:30:37

[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.coloradmin.cn/o/803942.html

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

相关文章

软件兼容性测试的重要性以及一些常用的测试方法

随着软件应用的不断发展&#xff0c;不同操作系统、浏览器、设备和平台的广泛应用&#xff0c;软件兼容性变得越来越重要。在开发和发布软件之前进行兼容性测试是确保软件在多个环境下正常运行的关键步骤。本文将介绍软件兼容性测试的重要性以及一些常用的测试方法。 首先&…

JMeter常用内置对象:vars、ctx、prev

在前文 Beanshell Sampler 与 Beanshell 断言 中&#xff0c;初步阐述了JMeter beanshell的使用&#xff0c;接下来归集整理了JMeter beanshell 中常用的内置对象及其使用。 注&#xff1a;示例使用JMeter版本为5.1 1. vars 如 API 文档 所言&#xff0c;这是定义变量的类&a…

SpringBoot版本升级引起的FileNotFoundException——WebMvcConfigurerAdapter.class

缘起 最近公司项目要求JDK从8升到17&#xff0c;SpringBoot版本从2.x升级到3.x&#xff0c;期间遇到了一个诡异的FileNotFoundException异常&#xff0c;日志如下&#xff08;敏感信息使用xxx脱敏&#xff09; org.springframework.beans.factory.BeanDefinitionStoreExcepti…

安科瑞智能型BA系列电流传感器

安科瑞虞佳豪 壹捌柒陆壹伍玖玖零玖叁 选型

微信小程序——同一控件的点击与长按事件共存的解决方案

✅作者简介&#xff1a;2022年博客新星 第八。热爱国学的Java后端开发者&#xff0c;修心和技术同步精进。 &#x1f34e;个人主页&#xff1a;Java Fans的博客 &#x1f34a;个人信条&#xff1a;不迁怒&#xff0c;不贰过。小知识&#xff0c;大智慧。 &#x1f49e;当前专栏…

一份 GitHub star 过万的 1121 页图解算法让“他”成功杀进字节跳动

前两天收到读者喜报&#xff0c;说是进字节了&#xff0c;和他交流了一下他的学习心得&#xff0c;发现他看的资料也是我之前推荐过的算法进阶指南&#xff0c;这里推荐给大家&#xff0c;github star 可是过万哦&#xff01;质量非常高&#xff01; 这份算法笔记与其他的不同&…

使用andlua+写一个获取VSCode最新版本号的安卓软件

点击加号 选择Defalut模板 名称改为vscv 包名改为com.b.vscv 编辑main.lua require "import" import "android.app.*" import "android.os.*" import "android.widget.*" import "android.view.*" import "layout&qu…

微信小程序开发总结

架构分析 软件应用架构包括&#xff1a; 数据层、业务逻辑层、服务处、控制层、展示层、用户&#xff0c;小程序属于展示层&#xff0c;通常还需要其他层次提供支持 主体文件&#xff1a; app.js,app.json,app.wxss&#xff0c;前两者是必须存在再根目录下&#xff0c;app.wxs…

【网络云盘客户端】——上传文件的功能的实现

目录 上传文件功能的实现 uploadtask的设计 设置上传的槽函数 uploadFileAction接口 uploadFile接口 定时上传文件 进度条的设计 上传文件功能的实现 上传文件功能实现 1.双击 ”上传文件 “的 QListWidgetItem 或者 点击 “上传” 菜单项 都会弹出一个文件对话框 2.在文…

关于Java中的Lambda变量捕获

博主简介&#xff1a;想进大厂的打工人博主主页&#xff1a;xyk:所属专栏: JavaEE进阶 目录 一、Lambda表达式语法 二、Lambda中变量捕获 一、Lambda表达式语法 基本语法: (parameters) -> expression 或 (parameters) ->{ statements; } Lambda表达式由三部分组成&a…

嵌入式:QT Day4

一、手动完成服务器的实现&#xff0c;并注释具体步骤 源码&#xff1a; widget.h #ifndef WIDGET_H #define WIDGET_H#include <QWidget> #include <QTcpServer> //服务器类 #include <QTcpSocket> //客户端类 #include <…

vue-echarts配置项详解

起因 最近接手了一个vue3项目&#xff0c;echarts用的是"vue-echarts": “^6.0.0”&#xff0c;每次查看文档的时候痛苦不已&#xff0c;找一个配置要花费大量时间&#xff0c;所以这篇文章&#xff0c;主要就是为了记录比较常见的一些配置。 主要会写三种图的配置…

libcomposite: Unknown symbol config_group_init (err 0)

加载libcomposite.ko 失败 问题描述 如图&#xff0c;在做USB OTG 设备模式的时候需要用到libcomposite.ko驱动&#xff0c;加载失败了。 原因&解决方法 有一个依赖叫configfs.ko的驱动没有安装。可以从内核代码的fs/configfs/configfs.ko中找到这个驱动。先加载confi…

Linux学习之自定义函数

函数是把一些重复使用的命令封装成一个集合&#xff0c;之后可以使用函数名调用。 定义函数的格式如下&#xff1a; function 函数名() {指令集&#xff08;若干条语句&#xff09; return n }要是直接在Shell中直接定义函数&#xff0c;那么直接在Shell中直接使用函数名 参数…

建筑工地为什么要做人员定位?解读技术背后的安全与效益

建筑工地是一个复杂而危险的环境&#xff0c;人员安全一直是行业亟待解决的难题。为了确保工人的安全&#xff0c;并提高工地的管理效率&#xff0c;越来越多的建筑工地开始采用人员定位技术。 对此&#xff0c;华安联大便和各位朋友一起深入探讨人员定位技术的优势和功能&…

C++(14):重载运算与类型转换

当运算符被用于类类型的对象时&#xff0c;允许我们为其指定新的含义&#xff1b;同时&#xff0c;也能自定义类类型之间的转换规则。和内置类型的转换一样&#xff0c;类类型转换隐式地将一种类型的对象转换成另一种我们所需类型的对象。 当运算符作用于类类型的运算对象时&a…

一、创建自己的docker python容器环境;支持新增python包并更新容器;离线打包、加载image

1、创建自己的docker python容器环境 参考&#xff1a;https://blog.csdn.net/weixin_42357472/article/details/118991485 首先写Dockfile&#xff0c;注意不要有txt等后缀 Dockfile # 使用 Python 3.9 镜像作为基础 FROM python:3.9# 设置工作目录 WORKDIR /app# 复制当前…

了解网络层

网络层 1. 概述2. 网络层提供的两种服务2.1 面向连接的虚电路服务2.2 无连接的数据报服务2.3 虚电路服务 VS 数据报服务 3. IPv4地址及其应用3.1 分类编址的IPv4地址3.1.1 A类地址3.1.2 B类地址3.1.3 C类地址 3.2 划分子网的IPv4地址3.3 无分类编址的IPv4地址3.4 IPv4地址的应用…

出海品牌整合营销指南:打造全球化成功的关键策略

随着全球化的不断深入&#xff0c;越来越多的企业开始将目光投向海外市场&#xff0c;希望在国际舞台上展现自己的实力和魅力。然而&#xff0c;出海市场的竞争激烈&#xff0c;如何在陌生的土地上建立起品牌影响力&#xff0c;成为摆在出海企业面前的一大难题。在这样的背景下…

TD1850多用表校准系统参考标准

参考标准 分类 标准名称 国家标准 GB/T 13978-2008 数字多用表 GB/T 15637-2012 数字多用表校准仪通用规范 计量法规 JJF 1075-2015 钳形电流表校准规范 JJF 1284-2011 交直流电表校验仪校准规范 JJF 1587-2016 数字多用表校准规范 JJG 124-2005 电流表、电压表、功率表及…