当前位置: 首页 > news >正文

C++初阶——list

一、什么是list

        list是一个可以在序列的任意位置进行插入和删除的容器,并且可以进行双向迭代。list的底层是一个双向链表,双向链表可以将它们包含的每个元素存储在不同且不相关的存储位置。通过将每个元素与前一个元素的链接和后一个元素的链接关联起来,内部保持了顺序。

二、list的构造

构造函数接口说明
list(size_type n,const value_type& val = valur_type())为构造的list,初始化n个空间,并根据第二个参数进行初始化
list()构造空的list
list(const list* x)拷贝构造函数
list(Inputlterator first, Inputlterator last)使用迭代器区间中的元素进行初始化

函数原型:

explicit list ();explicit list (size_type n, const value_type& val = value_type());template <class InputIterator>list (InputIterator first, InputIterator last);list (const list& x);

list的构造使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<int> first;//无参构造list<int> second(10, 1);//构造的list中包含10个1list<int> third(s.begin() + 5, s.end());//迭代器构造list<int> fourth(third);//拷贝构造函数return 0;
}

三、list的迭代器

        迭代器,按照访问方式可以分为以下三种:

  • 前向迭代器(Forward Iterators):它支持多次遍历,只能递增不能递减。
  • 双向迭代器(Bidirectional Iterators):与前向迭代器类似,但是既能递增也能递减。
  • 随机迭代器(Random Access Iterators):它支持所有双向迭代器的操作,支持跳过元素,支持下标访问,支持比较操作。

        list的迭代器属于双向迭代器,也就是说list不能像vector和数组那样通过[]进行访问。

函数声明接口说明
begin+endbegin返回第一个元素的迭代器,end返回逻辑上最后一个元素之后的位置
rbegin+rendrbegin返回最后一个元素的迭代器,这个迭代器指向的是理论上位于容器第一个元素之前的元素,即它不指向容器中的任何实际元素。

函数原型为:

iterator begin() noexcept;
const_iterator begin() const noexcept;iterator end() noexcept;
const_iterator end() const noexcept;reverse_iterator rbegin() noexcept;
const_reverse_iterator rbegin() const noexcept;reverse_iterator rend() nothrow;
const_reverse_iterator rend() const nothrow;

迭代器的使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(), s.end());list<char>::iterator it = first.begin();list<char>::reverse_iterator r_it = first.rbegin();cout << "正向迭代器:";while (it != first.end()){cout << *it << " ";++it;}cout << endl;cout << "反向迭代器:";while (r_it != first.rend()){cout << *r_it << " ";++r_it;}
}

 输出结果为:

四、list的容量相关

函数声明        接口说明
empty检测list是否为空,是返回true,否则返回false
size返回list中有效节点的个数

函数原型为: 

size_type size() const noexcept;bool empty() const noexcept;

函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{list<int> first;list<int> second(10, 1);cout << first.empty() << endl;cout << second.empty() << endl;cout << first.size() << endl;cout << second.size() << endl;
}

五、list的访问

函数声明接口说明
front返回lsit的第一个节点的值的引用
back返回list的最后一个节点的值的引用

函数原型为:

reference front();
const_reference front() const;reference back();
const_reference back() const;

 函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(),s.end());cout << first.front() << " " << first.back();
}

 输出结果为:

六、list的修改

函数声明接口说明
push_front在list首元素前插入一个值
pop_front删除list中的第一个元素
push_back在尾部插入一个值
pop_back删除list的最后一个元素
insert在给定的迭代器对应位置插入元素
erase删除迭代器对应的元素
swap交换两个list中的元素

push_front和pop_front

函数原型:

void push_front (const value_type& val);
void push_front (value_type&& val);void pop_front();

函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(),s.end());first.push_front('k');cout << first.front() << endl;first.pop_front();cout << first.front() << endl;
}

输出结果:

push_back和pop_back 

函数原型:

void push_back (const value_type& val);
void push_back (value_type&& val);void pop_back();

函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(),s.end());first.push_back('k');cout << first.back() << endl;first.pop_back();cout << first.back() << endl;
}

输出结果:

insert函数 

函数原型:

iterator insert (const_iterator position, const value_type& val);iterator insert (const_iterator position, size_type n, const value_type& val);template <class InputIterator>
iterator insert (const_iterator position, InputIterator first, InputIterator last);

函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(),s.end());first.insert(first.begin(), 'c');first.insert(first.begin(), 10, 'c');first.insert(first.begin(), s.begin(), s.end());
}

erase函数

函数原型:

iterator erase (const_iterator position);
iterator erase (const_iterator first, const_iterator last);

函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(),s.end());first.erase(first.begin(), first.end());
}

swap函数

函数原型:

void swap (list& x);

函数使用:

#include<iostream>
#include<list>
#include<string>
using namespace std;int main()
{string s("hello world");list<char> first(s.begin(),s.begin() + 5);list<char> second(s.begin() + 5, s.end());first.swap(second);
}

七、list的操作

函数声明接口说明
reverse反转容器中元素的顺序
sort排序list中的元素,默认升序
merge合并两个列表,它们必须是有序的
unique消除列表中的重复元素,必须是有序的
remove移除给定的元素
splice将一个列表中的一个或一部分元素转移到另一个元素中

reverse函数

函数原型:

void reverse() noexcept;

函数使用:

#include <list>
#include <iostream>
using namespace std;int main() {list<int> first = { 1, 3, 5 };first.reverse();for (auto i : first){cout << i << " ";}
}

输出结果为:

sort函数

函数原型:

void sort();template <class Compare>void sort (Compare comp);

函数使用:

#include <list>
#include <iostream>
using namespace std;int main() {list<int> first = { 4,5,2,1,7,5,9 };first.sort();//默认升序//也可以写成first.sort(less<int>());for (auto i : first){cout << i << " ";}cout << endl;first.sort(greater<int>());//改为降序for (auto i : first){cout << i << " ";}
}

 输出结果:

merge函数

函数原型

void merge (list& x);template <class Compare>void merge (list& x, Compare comp);

        comp是一个比较函数或比较器,它接受两个参数并返回一个布尔值,指示第一个参数是否应该在排序顺序中排在第二个参数之前。这个比较函数应该产生一个严格的弱排序。该函数将参数列表中序列x中的所有元素合并到当前列表中,同时保持排序顺序。两个列表在合并操作之前都必须根据comp提供的比较标准进行排序。

函数使用:

#include <list>
#include <iostream>
using namespace std;int main() {list<int> first= { 1, 3, 5 };list<int> second= { 2, 4, 6 };// 使用自定义比较函数将 list2 合并到 list1first.merge(second, less<int>());// 打印合并后的列表for (int num : first) {cout << num << " ";}cout << endl;// 合并后 list2 应该为空if (second.empty()) {cout << "合并后second 为空。" << endl;}return 0;
}

输出结果为:

unique函数

函数原型:

void unique();template <class BinaryPredicate>void unique (BinaryPredicate binary_pred);

函数使用:

#include <list>
#include <iostream>
using namespace std;
int main() {std::list<int> first = { 1, 2, 2, 3, 3, 3, 4, 4, 5 };first.unique();// 打印列表for (int num : first) {std::cout << num << " ";}cout << std::endl;// 使用带参数版本的 unique 函数list<int> second = { 1, 2, 2, 3, 3, 3, 4, 4, 5 };second.unique(std::equal_to<int>());// 打印列表for (int num : second) {std::cout << num << " ";}std::cout << std::endl;return 0;
}

输出结果为:

remove函数

函数原型:

void remove (const value_type& val);

函数使用:

#include <list>
#include <iostream>
using namespace std;
int main() {std::list<int> first = { 1, 2, 2, 3, 4, 2, 5 };first.remove(2);for (int num : first) {cout << num << " ";}cout << endl;return 0;
}

输出结果:

splice函数 

函数原型:

void splice (const_iterator position, list& x);void splice (const_iterator position, list& x, const_iterator i);void splice (const_iterator position, list& x,const_iterator first, const_iterator last);

代码使用:

#include <list>
#include <iostream>
using namespace std;int main() {list<int> first = { 1, 2, 3 };list<int> second = { 4, 5, 6 };first.splice(first.end(), second);cout << "first: ";for (int num : first) {cout << num << " ";}cout << endl;first.splice(first.end(), first, next(first.begin(), 1));cout << "first after moving second element: ";for (int num : first) {cout << num << " ";}cout << endl;first.splice(second.end(), first, first.begin(), next(first.begin(), 3));cout << "second after moving elements: ";for (int num : second) {cout << num << " ";}cout << endl;return 0;
}

输出结果为:

相关文章:

C++初阶——list

一、什么是list list是一个可以在序列的任意位置进行插入和删除的容器&#xff0c;并且可以进行双向迭代。list的底层是一个双向链表&#xff0c;双向链表可以将它们包含的每个元素存储在不同且不相关的存储位置。通过将每个元素与前一个元素的链接和后一个元素的链接关联起来&…...

软件设计师-排序算法

冒泡排序 每一趟冒泡排序&#xff0c;从第0个元素开始&#xff0c;和后面的元素比较&#xff0c;如果大于就交换&#xff0c;否则不变&#xff0c;每次冒泡可以把最大的元素放到最后一个&#xff0c;第一次冒泡的终点是n-1,第二趟的是n-2,直到最后剩下一个元素。时间复杂度O(n…...

即插即用篇 | YOLOv8 引入 代理注意力 AgentAttention

Transformer模型中的注意力模块是其核心组成部分。虽然全局注意力机制具有很强的表达能力,但其高昂的计算成本限制了在各种场景中的应用。本文提出了一种新的注意力范式,称为“代理注意力”(Agent Attention),以在计算效率和表示能力之间取得平衡。代理注意力使用四元组(Q…...

020_Servlet_Mysql学生选课系统(新版)_lwplus87

摘 要 随着在校大学生人数的不断增加&#xff0c;教务系统的数据量也不断的上涨。针对学生选课这一环节&#xff0c;本系统从学生网上自主选课以及课程发布两个大方面进行了设计&#xff0c;基本实现了学生的在线信息查询、选课功能以及教师对课程信息发布的管理等功能&…...

LabVIEW导入并显示CAD DXF文件图形 程序见附件

LabVIEW导入并显示CAD DXF文件图形 程序见附件 LabVIEW导入并显示CAD DXF文件图形 程序见附件 - 北京瀚文网星科技有限公司 LabVIEW广泛应用于自动化、数据采集、图形显示等领域。对于涉及CAD图形的应用&#xff0c;LabVIEW也提供了一些方法来导入和显示CAD DXF文件&#x…...

《云原生安全攻防》-- K8s安全防护思路

从本节课程开始&#xff0c;我们将正式进入防护篇。通过深入理解K8s提供的多种安全机制&#xff0c;从防守者的角度&#xff0c;运用K8s的安全最佳实践来保障K8s集群的安全。 在这个课程中&#xff0c;我们将学习以下内容&#xff1a; K8s安全防护思路&#xff1a;掌握K8s自身提…...

鸿蒙系统的发展及开发者机遇

鸿蒙系统&#xff08;HarmonyOS&#xff09;凭借其分布式架构和跨设备协同能力&#xff0c;展现出强大的发展潜力&#xff0c;在智能手机、智能穿戴、车载、家居等行业领域应用日益广泛&#xff0c;已逐渐形成与安卓、iOS 三足鼎立的市场格局。 开发者面临的挑战 1. 技术适应与…...

Java | Leetcode Java题解之第556题下一个更大元素III

题目&#xff1a; 题解&#xff1a; class Solution {public int nextGreaterElement(int n) {int x n, cnt 1;for (; x > 10 && x / 10 % 10 > x % 10; x / 10) {cnt;}x / 10;if (x 0) {return -1;}int targetDigit x % 10;int x2 n, cnt2 0;for (; x2 %…...

OSPF动态路由配置实验:实现高效网络自动化

实验主题&#xff1a;OSPF动态路由协议配置 实验背景 OSPF&#xff08;Open Shortest Path First&#xff09;是一种基于链路状态的路由协议&#xff0c;广泛应用于中大型网络中。它采用Dijkstra算法计算最短路径&#xff0c;以确保网络中的路由更新快速、稳定&#xff0c;并能…...

CRM对企业有什么用?如何在实践中有效应用CRM系统?

在现在非常激烈竞争环境中&#xff0c;客户关系管理系统&#xff08;CRM&#xff09; 已经成为很多企业的“必备神器”&#xff0c;它不仅帮助企业高效地管理客户信息&#xff0c;还能提高客户满意度&#xff0c;增强客户忠诚度&#xff0c;最终推动销售增长和业务发展。然而&a…...

渗透测试之 -- Linux基础

声明 学习视频来自B站UP主 泷羽sec,如涉及侵泷羽sec权马上删除文章笔记的只是方便各位师傅学习知识,以下网站涉及学习内容,其他的都与本人无关,切莫逾越法律红线,否则后果自负 一、Openssl 1、openssl passwd -1 123 openssl一个开源加密工具包&#xff0c;用于各种解密、加…...

【excel】easy excel如何导出动态列

动态也有多重含义&#xff1a;本文将描述两种动态场景下的解决方案 场景一&#xff1a;例如表头第一列固定为动物&#xff0c;且必定有第二列&#xff0c;第二列的表头可能为猫 也可能为狗&#xff1b;这是列数固定&#xff0c;列名不固定的场景&#xff1b; 场景二&#xff1…...

[Linux] 进程间通信

进程间通信&#xff08;Inter-Process Communication, IPC&#xff09;是指不同进程之间的数据交换与协作。在Linux中&#xff0c;进程间通信有多种方式&#xff0c;每种方式都有其适用的场景。本文将介绍Linux中常见的几种进程间通信方法&#xff1a;管道&#xff08;Pipe&…...

【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-最大的数

CL13 最大的数(20 分) 输入一个有 n 个无重复元素的整数数组 a&#xff0c;输出数组中最大的数。提示&#xff1a;如使用排序库函数 sort()&#xff0c;需要包含头文件#include 。输入&#xff1a; 第一行是一个正整数 n(2<n<20)&#xff1b; 第二行包含 n 个不重复的整…...

【Linux】sudo make install 命令往系统中安装了什么 指定目录进行安装

前情提要 假如我们通过源码安装的方式&#xff0c;安装一个动态库&#xff0c;风格往往是这样的&#xff1a; # 克隆仓库 git clone https://github.com/xxx.git# 进入仓库目录 cd xxx编译 # ... 可能有一些校验代码完整性的sh命令# 构建 mkdir build cd build cmake ..# 编…...

RT-DETR融合CVPR[2020]轻量化卷积模块Ghost Module模块

RT-DETR使用教程&#xff1a; RT-DETR使用教程 RT-DETR改进汇总贴&#xff1a;RT-DETR更新汇总贴 《GhostNet: More Features from Cheap Operations》 一、 模块介绍 论文链接&#xff1a;https://arxiv.org/abs/1911.11907 代码链接&#xff1a;GitHub - huawei-noah/Effici…...

发布rust crate

文章目录 一、cargo构建的配置类型&#xff1a;dev与release两种1.编译级别2.将 crate 发布到 Crates.io对整个库的注释pub use再导出功能发布crates.io 参考 一、cargo构建的配置类型&#xff1a;dev与release两种 $ cargo buildFinished dev [unoptimized debuginfo] targe…...

Sequelize+Sqlite3使用示例

以下是一个简单的示例&#xff0c;展示了如何在Node.js中使用Express框架、Sequelize ORM以及SQLite数据库来构建一个支持RESTful API的Web应用程序。 一&#xff0c;安装必要的npm包&#xff1a; npm install express sequelize sqlite3 body-parser 二&#xff0c;创建Jav…...

MyBatisPlus 用法详解

MyBatisPlus 用法详解 MyBatis-Plus&#xff08;简称MP&#xff09;是一个MyBatis的增强工具&#xff0c;在MyBatis的基础上只做增强不做改变&#xff0c;为简化开发、提高效率而生。它提供了丰富的功能&#xff0c;包括强大的CRUD操作、条件构造器、自动填充、分页插件等&…...

强化学习入门笔记(Reinforcement Learning,RL) 强推!

由于本人的近期研究方向涉及到强化学习&#xff0c;本科时已经学习过了&#xff0c;但是感觉还是有些概念和算法没有学懂学透&#xff0c;所以想重新系统性的学习一下&#xff0c;记录了整个学习过程&#xff0c;而且对当时没有理解不是特别深刻的内容有了一些更加深刻的理解&a…...

网络寻路--图论

所以我们固定题中M条边&#xff08;因为这M条一定联通&#xff09; P8605 [蓝桥杯 2013 国 AC] 网络寻路 - 洛谷 #include<bits/stdc.h> using namespace std; #define N 100011 typedef long long ll; typedef pair<int,int> pii; int n,m; int d[N],u[N],v[N]…...

C# Wkhtmltopdf HTML转PDF碰到的问题

最近碰到一个Html转PDF的需求&#xff0c;看了一下基本上都是需要依赖Wkhtmltopdf&#xff0c;需要在Windows或者linux安装这个可以后使用。找了一下选择了HtmlToPDFCore&#xff0c;这个库是对Wkhtmltopdf.NetCore简单二次封装&#xff0c;这个库的好处就是通过NuGet安装HtmlT…...

构建 MCP 服务器:第 4 部分 — 创建工具

这是我们构建 MCP 服务器的四部分教程的最后一部分。在第一部分中&#xff0c;我们使用基本资源创建了第一个 MCP 服务器。第二部分添加了资源模板并改进了代码组织。在第三部分中&#xff0c;我们添加了提示符并进一步完善了服务器结构。现在&#xff0c;我们将通过添加工具来…...

生成JavaDoc文档

生成 JavaDoc 文档 1、快速生成 文档 注解 2、常见的文档注解 3、脚本生成 doc 文档 4、IDEA工具栏生成 doc 文档 第一章 快速入门 第01节 使用插件 在插件工具当中&#xff0c;找到插件 javaDoc 使用方式&#xff0c;在代码区域&#xff0c;直接点击右键。选择 第02节 常用注…...

网络安全逆向分析之rust逆向技巧

rust逆向技巧 rust逆向三板斧&#xff1a; 快速定位关键函数 (真正的main函数)&#xff1a;观察输出、输入&#xff0c;字符串搜索&#xff0c;断点等方法。定位关键 加密区 &#xff1a;根据输入的flag&#xff0c;打硬件断点&#xff0c;快速捕获程序中对flag访问的位置&am…...

Vue 3 弹出式计算器组件(源码 + 教程)

&#x1f9ee; Vue 3 弹出式计算器组件&#xff08;源码 教程&#xff09; &#x1f4cc; 建议收藏 点赞 关注&#xff0c;本组件支持加减乘除、双向绑定、计算过程展示&#xff0c;适用于表单辅助输入场景。 &#x1f527; 一、完整源码&#xff08;复制即用&#xff09; …...

文件上传漏洞深度解析:检测与绕过技术矩阵

文件上传漏洞深度解析&#xff1a;检测与绕过技术矩阵 引言&#xff1a;无处不在的文件上传风险 在当今的Web应用生态系统中&#xff0c;文件上传功能几乎无处不在。从社交媒体分享图片到企业文档管理系统&#xff0c;用户上传文件已成为现代Web应用的核心功能之一。然而&…...

centos查看开启关闭防火墙状态

执行&#xff1a;systemctl status firewalld &#xff0c;即可查看防火墙状态 防火墙的开启、关闭、禁用命令 &#xff08;1&#xff09;设置开机启用防火墙&#xff1a;systemctl enable firewalld.service &#xff08;2&#xff09;设置开机禁用防火墙&#xff1a;system…...

三、元器件的选型

前言&#xff1a;我们确立了题目的功能后&#xff0c;就可以开始元器件的选型&#xff0c;元器件的选型关乎到我们后面代码编写的一个难易。 一、主控的选择 主控的选择很大程度上决定我们后续使用的代码编译器&#xff0c;比如ESP32使用的是VScode&#xff0c;或者Arduino&a…...

【学习笔记】单例类模板

【学习笔记】单例类模板 一、单例类模板 以下为一个通用的单例模式框架&#xff0c;这种设计允许其他类通过继承Singleton模板类来轻松实现单例模式&#xff0c;而无需为每个类重复编写单例实现代码。 // 命名空间&#xff08;Namespace&#xff09; 和 模板&#xff08;Tem…...