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…...
(LeetCode 每日一题) 3442. 奇偶频次间的最大差值 I (哈希、字符串)
题目:3442. 奇偶频次间的最大差值 I 思路 :哈希,时间复杂度0(n)。 用哈希表来记录每个字符串中字符的分布情况,哈希表这里用数组即可实现。 C版本: class Solution { public:int maxDifference(string s) {int a[26]…...
练习(含atoi的模拟实现,自定义类型等练习)
一、结构体大小的计算及位段 (结构体大小计算及位段 详解请看:自定义类型:结构体进阶-CSDN博客) 1.在32位系统环境,编译选项为4字节对齐,那么sizeof(A)和sizeof(B)是多少? #pragma pack(4)st…...
工程地质软件市场:发展现状、趋势与策略建议
一、引言 在工程建设领域,准确把握地质条件是确保项目顺利推进和安全运营的关键。工程地质软件作为处理、分析、模拟和展示工程地质数据的重要工具,正发挥着日益重要的作用。它凭借强大的数据处理能力、三维建模功能、空间分析工具和可视化展示手段&…...
1.3 VSCode安装与环境配置
进入网址Visual Studio Code - Code Editing. Redefined下载.deb文件,然后打开终端,进入下载文件夹,键入命令 sudo dpkg -i code_1.100.3-1748872405_amd64.deb 在终端键入命令code即启动vscode 需要安装插件列表 1.Chinese简化 2.ros …...
Maven 概述、安装、配置、仓库、私服详解
目录 1、Maven 概述 1.1 Maven 的定义 1.2 Maven 解决的问题 1.3 Maven 的核心特性与优势 2、Maven 安装 2.1 下载 Maven 2.2 安装配置 Maven 2.3 测试安装 2.4 修改 Maven 本地仓库的默认路径 3、Maven 配置 3.1 配置本地仓库 3.2 配置 JDK 3.3 IDEA 配置本地 Ma…...
C++.OpenGL (14/64)多光源(Multiple Lights)
多光源(Multiple Lights) 多光源渲染技术概览 #mermaid-svg-3L5e5gGn76TNh7Lq {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-3L5e5gGn76TNh7Lq .error-icon{fill:#552222;}#mermaid-svg-3L5e5gGn76TNh7Lq .erro…...
华为OD机考-机房布局
import java.util.*;public class DemoTest5 {public static void main(String[] args) {Scanner in new Scanner(System.in);// 注意 hasNext 和 hasNextLine 的区别while (in.hasNextLine()) { // 注意 while 处理多个 caseSystem.out.println(solve(in.nextLine()));}}priv…...
三分算法与DeepSeek辅助证明是单峰函数
前置 单峰函数有唯一的最大值,最大值左侧的数值严格单调递增,最大值右侧的数值严格单调递减。 单谷函数有唯一的最小值,最小值左侧的数值严格单调递减,最小值右侧的数值严格单调递增。 三分的本质 三分和二分一样都是通过不断缩…...
Golang——7、包与接口详解
包与接口详解 1、Golang包详解1.1、Golang中包的定义和介绍1.2、Golang包管理工具go mod1.3、Golang中自定义包1.4、Golang中使用第三包1.5、init函数 2、接口详解2.1、接口的定义2.2、空接口2.3、类型断言2.4、结构体值接收者和指针接收者实现接口的区别2.5、一个结构体实现多…...
嵌入式常见 CPU 架构
架构类型架构厂商芯片厂商典型芯片特点与应用场景PICRISC (8/16 位)MicrochipMicrochipPIC16F877A、PIC18F4550简化指令集,单周期执行;低功耗、CIP 独立外设;用于家电、小电机控制、安防面板等嵌入式场景8051CISC (8 位)Intel(原始…...
