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+end | begin返回第一个元素的迭代器,end返回逻辑上最后一个元素之后的位置 |
rbegin+rend | rbegin返回最后一个元素的迭代器,这个迭代器指向的是理论上位于容器第一个元素之前的元素,即它不指向容器中的任何实际元素。 |
函数原型为:
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是一个可以在序列的任意位置进行插入和删除的容器,并且可以进行双向迭代。list的底层是一个双向链表,双向链表可以将它们包含的每个元素存储在不同且不相关的存储位置。通过将每个元素与前一个元素的链接和后一个元素的链接关联起来&…...
软件设计师-排序算法
冒泡排序 每一趟冒泡排序,从第0个元素开始,和后面的元素比较,如果大于就交换,否则不变,每次冒泡可以把最大的元素放到最后一个,第一次冒泡的终点是n-1,第二趟的是n-2,直到最后剩下一个元素。时间复杂度O(n…...

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

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

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

《云原生安全攻防》-- K8s安全防护思路
从本节课程开始,我们将正式进入防护篇。通过深入理解K8s提供的多种安全机制,从防守者的角度,运用K8s的安全最佳实践来保障K8s集群的安全。 在这个课程中,我们将学习以下内容: K8s安全防护思路:掌握K8s自身提…...
鸿蒙系统的发展及开发者机遇
鸿蒙系统(HarmonyOS)凭借其分布式架构和跨设备协同能力,展现出强大的发展潜力,在智能手机、智能穿戴、车载、家居等行业领域应用日益广泛,已逐渐形成与安卓、iOS 三足鼎立的市场格局。 开发者面临的挑战 1. 技术适应与…...

Java | Leetcode Java题解之第556题下一个更大元素III
题目: 题解: 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动态路由配置实验:实现高效网络自动化
实验主题:OSPF动态路由协议配置 实验背景 OSPF(Open Shortest Path First)是一种基于链路状态的路由协议,广泛应用于中大型网络中。它采用Dijkstra算法计算最短路径,以确保网络中的路由更新快速、稳定,并能…...

CRM对企业有什么用?如何在实践中有效应用CRM系统?
在现在非常激烈竞争环境中,客户关系管理系统(CRM) 已经成为很多企业的“必备神器”,它不仅帮助企业高效地管理客户信息,还能提高客户满意度,增强客户忠诚度,最终推动销售增长和业务发展。然而&a…...

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

【excel】easy excel如何导出动态列
动态也有多重含义:本文将描述两种动态场景下的解决方案 场景一:例如表头第一列固定为动物,且必定有第二列,第二列的表头可能为猫 也可能为狗;这是列数固定,列名不固定的场景; 场景二࿱…...
[Linux] 进程间通信
进程间通信(Inter-Process Communication, IPC)是指不同进程之间的数据交换与协作。在Linux中,进程间通信有多种方式,每种方式都有其适用的场景。本文将介绍Linux中常见的几种进程间通信方法:管道(Pipe&…...

【蓝桥等考C++真题】蓝桥杯等级考试C++组第13级L13真题原题(含答案)-最大的数
CL13 最大的数(20 分) 输入一个有 n 个无重复元素的整数数组 a,输出数组中最大的数。提示:如使用排序库函数 sort(),需要包含头文件#include 。输入: 第一行是一个正整数 n(2<n<20); 第二行包含 n 个不重复的整…...

【Linux】sudo make install 命令往系统中安装了什么 指定目录进行安装
前情提要 假如我们通过源码安装的方式,安装一个动态库,风格往往是这样的: # 克隆仓库 git clone https://github.com/xxx.git# 进入仓库目录 cd xxx编译 # ... 可能有一些校验代码完整性的sh命令# 构建 mkdir build cd build cmake ..# 编…...

RT-DETR融合CVPR[2020]轻量化卷积模块Ghost Module模块
RT-DETR使用教程: RT-DETR使用教程 RT-DETR改进汇总贴:RT-DETR更新汇总贴 《GhostNet: More Features from Cheap Operations》 一、 模块介绍 论文链接:https://arxiv.org/abs/1911.11907 代码链接:GitHub - huawei-noah/Effici…...

发布rust crate
文章目录 一、cargo构建的配置类型:dev与release两种1.编译级别2.将 crate 发布到 Crates.io对整个库的注释pub use再导出功能发布crates.io 参考 一、cargo构建的配置类型:dev与release两种 $ cargo buildFinished dev [unoptimized debuginfo] targe…...
Sequelize+Sqlite3使用示例
以下是一个简单的示例,展示了如何在Node.js中使用Express框架、Sequelize ORM以及SQLite数据库来构建一个支持RESTful API的Web应用程序。 一,安装必要的npm包: npm install express sequelize sqlite3 body-parser 二,创建Jav…...
MyBatisPlus 用法详解
MyBatisPlus 用法详解 MyBatis-Plus(简称MP)是一个MyBatis的增强工具,在MyBatis的基础上只做增强不做改变,为简化开发、提高效率而生。它提供了丰富的功能,包括强大的CRUD操作、条件构造器、自动填充、分页插件等&…...

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

观成科技:隐蔽隧道工具Ligolo-ng加密流量分析
1.工具介绍 Ligolo-ng是一款由go编写的高效隧道工具,该工具基于TUN接口实现其功能,利用反向TCP/TLS连接建立一条隐蔽的通信信道,支持使用Let’s Encrypt自动生成证书。Ligolo-ng的通信隐蔽性体现在其支持多种连接方式,适应复杂网…...

树莓派超全系列教程文档--(61)树莓派摄像头高级使用方法
树莓派摄像头高级使用方法 配置通过调谐文件来调整相机行为 使用多个摄像头安装 libcam 和 rpicam-apps依赖关系开发包 文章来源: http://raspberry.dns8844.cn/documentation 原文网址 配置 大多数用例自动工作,无需更改相机配置。但是,一…...
线程与协程
1. 线程与协程 1.1. “函数调用级别”的切换、上下文切换 1. 函数调用级别的切换 “函数调用级别的切换”是指:像函数调用/返回一样轻量地完成任务切换。 举例说明: 当你在程序中写一个函数调用: funcA() 然后 funcA 执行完后返回&…...

大数据零基础学习day1之环境准备和大数据初步理解
学习大数据会使用到多台Linux服务器。 一、环境准备 1、VMware 基于VMware构建Linux虚拟机 是大数据从业者或者IT从业者的必备技能之一也是成本低廉的方案 所以VMware虚拟机方案是必须要学习的。 (1)设置网关 打开VMware虚拟机,点击编辑…...

前端导出带有合并单元格的列表
// 导出async function exportExcel(fileName "共识调整.xlsx") {// 所有数据const exportData await getAllMainData();// 表头内容let fitstTitleList [];const secondTitleList [];allColumns.value.forEach(column > {if (!column.children) {fitstTitleL…...
Golang dig框架与GraphQL的完美结合
将 Go 的 Dig 依赖注入框架与 GraphQL 结合使用,可以显著提升应用程序的可维护性、可测试性以及灵活性。 Dig 是一个强大的依赖注入容器,能够帮助开发者更好地管理复杂的依赖关系,而 GraphQL 则是一种用于 API 的查询语言,能够提…...

相机从app启动流程
一、流程框架图 二、具体流程分析 1、得到cameralist和对应的静态信息 目录如下: 重点代码分析: 启动相机前,先要通过getCameraIdList获取camera的个数以及id,然后可以通过getCameraCharacteristics获取对应id camera的capabilities(静态信息)进行一些openCamera前的…...
论文解读:交大港大上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一)
宇树机器人多姿态起立控制强化学习框架论文解析 论文解读:交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化学习框架(一) 论文解读:交大&港大&上海AI Lab开源论文 | 宇树机器人多姿态起立控制强化…...

学校时钟系统,标准考场时钟系统,AI亮相2025高考,赛思时钟系统为教育公平筑起“精准防线”
2025年#高考 将在近日拉开帷幕,#AI 监考一度冲上热搜。当AI深度融入高考,#时间同步 不再是辅助功能,而是决定AI监考系统成败的“生命线”。 AI亮相2025高考,40种异常行为0.5秒精准识别 2025年高考即将拉开帷幕,江西、…...
JAVA后端开发——多租户
数据隔离是多租户系统中的核心概念,确保一个租户(在这个系统中可能是一个公司或一个独立的客户)的数据对其他租户是不可见的。在 RuoYi 框架(您当前项目所使用的基础框架)中,这通常是通过在数据表中增加一个…...