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…...
HTML 语义化
目录 HTML 语义化HTML5 新特性HTML 语义化的好处语义化标签的使用场景最佳实践 HTML 语义化 HTML5 新特性 标准答案: 语义化标签: <header>:页头<nav>:导航<main>:主要内容<article>&#x…...
学习STC51单片机31(芯片为STC89C52RCRC)OLED显示屏1
每日一言 生活的美好,总是藏在那些你咬牙坚持的日子里。 硬件:OLED 以后要用到OLED的时候找到这个文件 OLED的设备地址 SSD1306"SSD" 是品牌缩写,"1306" 是产品编号。 驱动 OLED 屏幕的 IIC 总线数据传输格式 示意图 …...
C++ 求圆面积的程序(Program to find area of a circle)
给定半径r,求圆的面积。圆的面积应精确到小数点后5位。 例子: 输入:r 5 输出:78.53982 解释:由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982,因为我们只保留小数点后 5 位数字。 输…...
CMake 从 GitHub 下载第三方库并使用
有时我们希望直接使用 GitHub 上的开源库,而不想手动下载、编译和安装。 可以利用 CMake 提供的 FetchContent 模块来实现自动下载、构建和链接第三方库。 FetchContent 命令官方文档✅ 示例代码 我们将以 fmt 这个流行的格式化库为例,演示如何: 使用 FetchContent 从 GitH…...
使用 SymPy 进行向量和矩阵的高级操作
在科学计算和工程领域,向量和矩阵操作是解决问题的核心技能之一。Python 的 SymPy 库提供了强大的符号计算功能,能够高效地处理向量和矩阵的各种操作。本文将深入探讨如何使用 SymPy 进行向量和矩阵的创建、合并以及维度拓展等操作,并通过具体…...
PAN/FPN
import torch import torch.nn as nn import torch.nn.functional as F import mathclass LowResQueryHighResKVAttention(nn.Module):"""方案 1: 低分辨率特征 (Query) 查询高分辨率特征 (Key, Value).输出分辨率与低分辨率输入相同。"""def __…...
C/C++ 中附加包含目录、附加库目录与附加依赖项详解
在 C/C 编程的编译和链接过程中,附加包含目录、附加库目录和附加依赖项是三个至关重要的设置,它们相互配合,确保程序能够正确引用外部资源并顺利构建。虽然在学习过程中,这些概念容易让人混淆,但深入理解它们的作用和联…...
[免费]微信小程序问卷调查系统(SpringBoot后端+Vue管理端)【论文+源码+SQL脚本】
大家好,我是java1234_小锋老师,看到一个不错的微信小程序问卷调查系统(SpringBoot后端Vue管理端)【论文源码SQL脚本】,分享下哈。 项目视频演示 【免费】微信小程序问卷调查系统(SpringBoot后端Vue管理端) Java毕业设计_哔哩哔哩_bilibili 项…...
基于Java+VUE+MariaDB实现(Web)仿小米商城
仿小米商城 环境安装 nodejs maven JDK11 运行 mvn clean install -DskipTestscd adminmvn spring-boot:runcd ../webmvn spring-boot:runcd ../xiaomi-store-admin-vuenpm installnpm run servecd ../xiaomi-store-vuenpm installnpm run serve 注意:运行前…...
sshd代码修改banner
sshd服务连接之后会收到字符串: SSH-2.0-OpenSSH_9.5 容易被hacker识别此服务为sshd服务。 是否可以通过修改此banner达到让人无法识别此服务的目的呢? 不能。因为这是写的SSH的协议中的。 也就是协议规定了banner必须这么写。 SSH- 开头,…...
