[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介绍
- list是可以在常量时间内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。
- list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。
- list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高效。
- 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率更好。
- 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置。
2. 构造函数
(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中尾插数据1v.push_back(2); //在list中尾插数据2v.push_back(3); //在list中尾插数据3v.push_back(4); //在list中尾插数据4list<int> l(v.begin(), v.end());list<int>::iterator it = l.begin();while (it != l.end()){cout << *it << " ";it++;}cout << endl; //输出为 1 2 3 4return 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中尾插数据1v.push_back(2); //在list中尾插数据2v.push_back(3); //在list中尾插数据3v.push_back(4); //在list中尾插数据4list<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 1return 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;//输出为1cout << l2.empty() << endl;//输出为0return 0;
}
size函数
获取list存储的结点个数。
#include <iostream>
#include <list>
#include <vector>
using namespace std;
int main()
{vector<int> v;v.push_back(1); //在list中尾插数据1v.push_back(2); //在list中尾插数据2v.push_back(3); //在list中尾插数据3v.push_back(4); //在list中尾插数据4list<int> l(v.begin(), v.end());cout << l.size() << endl; //输出为4return 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; //输出为3return 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; //输出为3l.pop_back();l.pop_back();cout << l.size() << endl; //输出为1l.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 1return 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 1return 0;
}
insert函数和erase函数
和vector容器类似,list容器也没有提供find函数,而insert函数和erase函数是需要配合迭代器使用的,因此需要使用算法库的find函数。
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 4return 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 4return 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 3return 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 3return 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 5return 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函数
- 如果n < size, 会将结点删除至list只有n个结点,由于list结点的释放成本是不高的,因此不同于vector只是将末尾指向修改,list会实际上的在调用resize函数时的删除结点。
- 如果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; //输出为5l.resize(3); // n < sizecout << l.size() << endl; //输出为3l.resize(6); // n > sizecout << l.size() << endl; //输出为6return 0;
}
clear函数
释放所有结点,使得list为空链表。
#include <iostream>
#include <list>
using namespace std;
int main()
{list<int> l(66, 6);cout << l.size() << endl; // 输出为66l.clear();cout << l.size() << endl; // 输出为0return 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 9it++;}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 4it++;}l.reverse(); //将list逆置it = l.begin();while (it != l.end()){cout << *it << ' '; //输出为: 4 3 2 1it++;}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 4it++;}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 4it++;}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 4it++;}return 0;
}
相关文章:

[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…...

k8s服务发现之第五弹--使用 Service 连接到应用
Kubernetes 的网络模型 通过前面教程的学习,我们已经可以将容器化的应用程序在 Kubernetes 中运行起来,并且发布到 Kubernetes 内/外的网络上。 通常,Docker 使用一种 host-private 的联网方式,在此情况下,只有两个容…...

SAP ABAP 自定义表数据导入
一:效果展示: 读取 Excel 数据到 SAP 数据库表。 二:源码: *&---------------------------------------------------------------------* *& Report ZTEST_DRW02 *&----------------------------------------------------------…...

目标检测识别——大恒(DaHeng)相机操作与控制编程
文章目录 引言正文相关开发库的介绍编程准备配置引用头文件GalaxyIncludes.h配置lib文件 具体编程过程初始化和反初始化枚举设备开关设备 属性控制属性控制器种类 图像采集控制和图像处理采单帧回调采集 总结 引言 在做老师的横向项目时,需要用大恒相机,…...

国标GB28181视频监控平台EasyGBS视频无法播放,抓包返回ICMP是什么原因?
国标GB28181视频平台EasyGBS是基于国标GB/T28181协议的行业内安防视频流媒体能力平台,可实现的视频功能包括:实时监控直播、录像、检索与回看、语音对讲、云存储、告警、平台级联等功能。国标GB28181视频监控平台部署简单、可拓展性强,支持将…...

如何正确使用npm常用命令
npm常用命令: 官方文档:CLI Commands | npm Docs 1. npm -v:查看 npm 版本 2. npm init:初始化后会出现一个 Package.json 配置文件,可以在后面加上 -y,快速跳到问答界面 3. npm install:会…...

无人机影像配准并发布(共线方程)
无人机影像 DEM 计算四个角点坐标(刚性变换) 像空间坐标(x,y,-f) 像空间坐标畸变纠正 deltax,deltay 已知(x,y),求解(X,Y, Z)或者(Lat,Lon) 这里的Z是DEM上获取的坐标和Zs为相机坐标的高程,如果均为已…...

openGauss学习笔记-23 openGauss 简单数据管理-时间/日期函数和操作符
文章目录 openGauss学习笔记-23 openGauss 简单数据管理-时间/日期函数和操作符23.1 时间日期操作符23.2 时间/日期函数23.3 TIMESTAMPDIFF23.4 EXTRACT23.5 date_part openGauss学习笔记-23 openGauss 简单数据管理-时间/日期函数和操作符 23.1 时间日期操作符 用户在使用时…...

C++OpenCV(7):图像形态学基础操作
🔆 文章首发于我的个人博客:欢迎大佬们来逛逛 🔆 OpenCV项目地址及源代码:点击这里 文章目录 膨胀与腐蚀形态学基础 膨胀与腐蚀 膨胀与腐蚀是数学形态学在图像处理中最基础的操作。 膨胀操作是取每个位置领域内最大值࿰…...

Appium+python自动化(二十二)- 控件坐标获取(超详解)
简介 有些小伙伴或者是童鞋可能会好奇会问上一篇中的那个monkey脚本里的坐标点是如何获取的,不是自己随便蒙的猜的,或者是自己用目光或者是尺子量出来的吧,答案当然是:NO。获取控件坐标点的方式这里宏哥给小伙伴们分享和讲解三种方…...

Tensorflow benchmark 实操指南
环境搭建篇见环境搭建-CentOS7下Nvidia Docker容器基于TensorFlow1.15测试GPU_东方狱兔的博客-CSDN博客 1. 下载Benchmarks源码 从 TensorFlow 的 Github 仓库上下载 TensorFlow Benchmarks,可以通过以下命令来下载 https://github.com/tensorflow/benchmarks 我…...

【linux】调试工具介绍
文章目录 前言一、kdb二、ftrace三、gdb 前言 在Linux内核调试过程中,可以使用各种工具和技术来诊断和解决问题。以下是一些常用的Linux内核调试方法: printk:printk是Linux内核中的打印函数,可以在代码中插入打印语句来输出调试…...

2.获取DOM元素
获取DOM元素就是利用JS选择页面中的标签元素 2.1 根据CSS选择器来获取DOM元素(重点) 2.1.1选择匹配的第一个元素 语法: document.querySelector( css选择器 )参数: 包含一个或多个有效的CSS选择器 字符串 返回值: CSS选择器匹配的第一个元素,一个HTMLElement对象…...

flask中redirect、url_for、endpoint介绍
flask中redirect、url_for、endpoint介绍 redirect 在 Flask 中,redirect() 是一个非常有用的函数,可以使服务器发送一个HTTP响应,指示客户端(通常是浏览器)自动导航到新的 URL。基本上,它是用来重定向用…...

《MySQL》第十二篇 数据类型
目录 一. 整数类型二. 浮点类型三. 日期和时间类型四. 字符串类型五. 枚举值类型六. 二进制类型七. 小结 MySQL 支持多种数据类型,学习好数据类型,才能更好的学习 MySQL 表的设计,让表的设计更加合理。 一. 整数类型 类型大小SIGNED(有符号)…...

Python与OpenCV环境中,借助SIFT、单应性、KNN以及Ransac技术进行实现的图像拼接算法详细解析及应用
一、引言 在当今数字化时代,图像处理技术的重要性不言而喻。它在无人驾驶、计算机视觉、人脸识别等领域发挥着关键作用。作为图像处理的一个重要部分,图像拼接算法是实现广阔视野图像的重要手段。今天我们将会讲解在Python和OpenCV环境下,如何使用SIFT、单应性、KNN以及Ran…...

苍穹外卖Day01项目日志
1.软件开发流程和人员分工是怎样的? 软件开发流程 一个软件是怎么被开发出来的? 需求分析 先得知道软件定位人群、用户群体、有什么功能、要实现什么效果等。 需要得到需求规格说明书、产品原型。 需求规格说明书 其中前后端工程师要关注的就是产品原…...

Netty学习(二)
文章目录 二. Netty 入门1. 概述1.1 Netty 是什么?1.2 Netty 的作者1.3 Netty 的地位1.4 Netty 的优势 2. Hello World2.1 目标加入依赖 2.2 服务器端2.3 客户端2.4 流程梳理课堂示例服务端客户端 分析提示(重要) 3. 组件3.1 EventLoop事件循…...

ReactRouterv5在BrowserRouter和HashRouter模式下对location.state的支持
结论:HashRouter不支持location.state 文档:ReactRouter v5 从文档可看到history.push()方法支持2个参数:path, [state] state即是location.state,常用于隐式地传递状态参数 但文档未提的是,仅适用于BrowserRouter&am…...

Aerotech系列文章(3)运动设置命令Motion Setup Commands
1.运动设置命令Motion Setup Commands 斜坡类型: 直线,S曲线,与正弦曲线 Enumerator: RAMPTYPE_Linear Linear-based ramp type. RAMPTYPE_Scurve S-curve-based ramp type. RAMPTYPE_Sine Sine-based ramp type. 函数原型&a…...

线性神经网络——softmax 回归随笔【深度学习】【PyTorch】【d2l】
文章目录 3.2、softmax 回归3.2.1、softmax运算3.2.2、交叉熵损失函数3.2.3、PyTorch 从零实现 softmax 回归3.2.4、简单实现 softmax 回归 3.2、softmax 回归 3.2.1、softmax运算 softmax 函数是一种常用的激活函数,用于将实数向量转换为概率分布向量。它在多类别…...

【Nodejs】Node.js开发环境安装
1.版本介绍 在命令窗口中输入 node -v 可以查看版本 0.x 完全不技术 ES64.x 部分支持 ES6 特性5.x 部分支持ES6特性(比4.x多些),属于过渡产品,现在来说应该没有什么理由去用这个了6.x 支持98%的 ES6 特性8.x 支持 ES6 特性 2.No…...

梅尔频谱(Mel spectrum)简介及Python实现
梅尔频谱(Mel spectrum)简介及Python实现 1. 梅尔频谱(Mel spectrum)简介2. Python可视化测试3.频谱可视化3.1 Mel 频谱可视化3.2 STFT spectrum参考文献资料1. 梅尔频谱(Mel spectrum)简介 在信号处理上,声信号(噪声信号)是一种重要的传感监测手段。对于语音分类任务…...

【数据结构】实验六:队列
实验六 队列 一、实验目的与要求 1)熟悉C/C语言(或其他编程语言)的集成开发环境; 2)通过本实验加深对队列的理解,熟悉基本操作; 3) 结合具体的问题分析算法时间复杂度。 二、…...

【Linux线程】第一章||理解线程概念+创建一个线程(附代码加讲解)
线程概念 🌵什么是线程🌲线程和进程的关系🎄线程有以下特点:🌳 线程的优点🌴 线程的缺点🌱线程异常🌿线程用途 ☘️手动创建一个进程🍀运行 🌵什么是线程 在L…...

Android进阶之微信扫码登录
遇到新需求要搭建微信扫码登录功能,这篇文章是随着我的编码过程一并写的,希望能够帮助有需求的人和以后再次用到此功能的自己。 首先想到的就是百度各种文章,当然去开发者平台申请AppID和密钥是必不可少的,等注册好发现需要创建应用以及审核(要官网,流程图及其他信息),想着先写…...

macOS Monterey 12.6.8 (21G725) Boot ISO 原版可引导镜像
macOS Monterey 12.6.8 (21G725) Boot ISO 原版可引导镜像 本站下载的 macOS 软件包,既可以拖拽到 Applications(应用程序)下直接安装,也可以制作启动 U 盘安装,或者在虚拟机中启动安装。另外也支持在 Windows 和 Lin…...

Unity自定义后处理——用偏导数求图片颜色边缘
大家好,我是阿赵。 继续介绍屏幕后处理效果的做法。这次介绍一下用偏导数求图形边缘的技术。 一、原理介绍 先来看例子吧。 这个例子看起来好像是要给模型描边。之前其实也介绍过很多描边的方法,比如沿着法线方向放大模型,或者用Ndo…...

本地Git仓库和GitHub仓库SSH传输
SSH创建命令解释 ssh-keygen 用于创建密钥的程序 -m PEM 将密钥的格式设为 PEM -t rsa 要创建的密钥类型,本例中为 RSA 格式 -b 4096 密钥的位数,本例中为 4096 -C “azureusermyserver” 追加到公钥文件末尾以便于识别的注释。 通常以电子邮件地址…...

【C++11】——右值引用、移动语义
目录 1. 基本概念 1.1 左值与左值引用 1.2 右值和右值引用 1.3 左值引用与右值引用 2. 右值引用实用场景和意义 2.1 左值引用的使用场景 2.2 左值引用的短板 2.3 右值引用和移动语义 2.3.1 移动构造 2.3.2 移动赋值 2.3.3 编译器做的优化 2.3.4 总结 2.4 右值引用…...