《C++ list 完全指南:从基础到高效使用》

《C++ list 完全指南:从基础到高效使用》


文章目录

  • 《C++ list 完全指南:从基础到高效使用》
  • 一、forward_list和list比较
  • 二、list的接口介绍
    • 1.list的构造
    • 2.list iterator的使用
    • 3.list的容量操作
    • 4.list的访问操作
    • 5.list的其他操作接口
  • 三、list的迭代器失效
  • 四、list与vector的对比
  • 五、源代码总结


一、forward_list和list比较

在这里插入图片描述
在这里插入图片描述在这里插入图片描述


二、list的接口介绍

1.list的构造

在这里插入图片描述
在这里插入图片描述


2.list iterator的使用

迭代器可以理解成一个指针,该指针指向list中的某个节点!
在这里插入图片描述


在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


3.list的容量操作

这里指介绍重要的接口,一些不重要的接口可以参考string和vector
![在这里插入图片描述](https://i-blog.csdnimg.cn/direct/be8ef05c4dfa4b4cae4a9d180b54ddfb.png在这里插入图片描述
在这里插入图片描述


resize的用法和string、vector几乎相同,这里不再过多赘述
在这里插入图片描述


4.list的访问操作

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述


迭代器失效问题一会就会进行讲解!
在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


5.list的其他操作接口

在这里插入图片描述


splice()函数主要用于在列表中进行元素的转移操作。
它可以将一个列表中的部分或全部元素转移到另一个列表中。
可以指定要转移的元素范围以及目标插入位置等,实现了高效灵活的元素移动和重组

在这里插入图片描述


remove函数相当于一直遍历列表,然后erase删除指定元素
在这里插入图片描述


remove_if 函数 相当于 remove 的补充,它支持传参函数或者仿函数
在这里插入图片描述


unique函数主要用于移除列表中相邻的重复元素。
他使得容器中只保留不重复的元素序列,
但需要注意:他并不保证取出所有重复元素,知识处理相邻的重复项,通常也需要结合其他操作

在这里插入图片描述


merge( )函数主要用于将两个已排序的序列合并成一个新的已排序序列
他会按照排序顺序将一个序列中的元素与另一个序列中的元素合理地组合在一起,形成一个合并后的有序序列

需要注意的是,在合并之前,两个源序列本身需要时已经排序好的
在这里插入图片描述


list中的sort( )函数用于对列表进行排序。他会按照指定的排序规则(默认是升序)对列表中的元素进行重新排列,使得元素按有序多的方式呈现!
在这里插入图片描述


reverse( )函数,用于实现list的逆置
在这里插入图片描述


三、list的迭代器失效

在这里插入图片描述
在这里插入图片描述


在这里插入图片描述


四、list与vector的对比

在这里插入图片描述
在这里插入图片描述


五、源代码总结

代码如下(示例):

//#include <iostream>
//using namespace std;
//#include <forward_list>
//int main()
//{
//    forward_list<int> fl = { 1, 2, 3 };
//    // 在头部插入元素
//    fl.push_front(0);
//
//    // 遍历并输出
//    for (int num : fl)
//    {
//        cout << num << " ";
//    }
//    cout << endl;
//
//    return 0;
//}#include <iostream>
#include <list>
using namespace std;
//int main()
//{
//    list<int> myList = { 10, 20, 30, 40, 50 };
//    // 在头部插入元素
//    myList.push_front(5);
//    // 在尾部插入元素
//    myList.push_back(60);
//    // 遍历并输出
//    for (int num : myList) {
//        cout << num << " ";
//    }
//    cout << endl;
//
//    // 删除指定元素
//    myList.remove(30);
//
//    // 再次遍历输出
//    for (int num : myList) {
//        cout << num << " ";
//    }
//    cout << endl;
//    return 0;
//}void Test1()
{// 默认构造函数list<int> numbers1;cout << "默认构造: ";for (const auto& num : numbers1) {cout << num << " ";}cout << endl;// n个val构造list<int> numbers2(5, 10);cout << "n个val构造: ";for (const auto& num : numbers2) {cout << num << " ";}cout << endl;int arr[] = { 1, 2, 3 };// 通过vector的迭代器初始化list<int> numbers3(arr, arr + sizeof(arr) / sizeof(arr[0]));cout << "迭代器区间构造: ";for (const auto& num : numbers3) {cout << num << " ";}cout << endl;list<int> numbers4 = { 4, 5, 6 };// 拷贝构造list<int> numbers5(numbers4);cout << "拷贝构造: ";for (const auto& num : numbers5) {cout << num << " ";}cout << endl;numbers1 = numbers2;// 赋值重载cout << "赋值重载: ";for (const auto& num : numbers1) {cout << num << " ";}cout << endl;
}
void Test2()
{list<int> numbers = { 1, 2, 3, 4, 5 };list<int>::iterator it = numbers.begin();cout << "First element: " << *it << endl;while (it != numbers.end()){cout << *it << " ";++it;}// 注意:这里不能直接解引用it,因为此时它指向的是头节点cout << endl;
}
void Test3()
{list<int> numbers = { 1, 2, 3, 4, 5 };list<int>::reverse_iterator rit = numbers.rbegin();cout << "Last element: " << *rit << endl;while (rit != numbers.rend()){cout << *rit << " ";++rit;}cout << endl;
}
void Test4()
{list<int> numbers = { 1, 2, 3, 4, 5 };cout << "Size of list: " << numbers.size() << endl;cout << "Max size of list: " << numbers.max_size() << endl;
}void Test5()
{list<int> numbers;if (numbers.empty()){cout << "List is empty" << endl;}numbers = { 1, 2, 3 };numbers.clear();if (numbers.empty()){cout << "List is cleared and now empty" << endl;}
}void Test6()
{list<int> numbers = { 1, 2, 3 };numbers.resize(5);cout << "After resizing to 5: ";for (auto& num : numbers){cout << num << " ";}cout << endl;numbers.resize(2);cout << "After resizing to 2: ";for (auto& num : numbers){cout << num << " ";}cout << endl;
}
void Test7()
{list<int> myList = { 10, 20, 30 };  // 创建一个包含元素的列表// 输出列表的第一个元素cout << "The front element is: " << myList.front() << endl;// 输出列表的最后一个元素cout << "The back element is: " << myList.back() << endl;
}void Test8()
{list<int> myList;  // 创建一个空列表myList.push_back(10);  // 在列表尾部添加元素 10myList.push_back(20);  // 在列表尾部添加元素 20cout << "列表元素: ";for (auto& num : myList) {cout << num << " ";}cout << endl;myList.pop_back();  // 删除列表尾部的元素cout << "删除尾部元素后列表元素: ";for (auto& num : myList) {cout << num << " ";}cout << endl;
}
void Test9()
{list<int> myList;  // 创建一个空列表myList.push_front(5);  // 在列表头部添加元素 5myList.push_front(3);  // 在列表头部添加元素 3cout << "列表元素: ";for (auto& num : myList) {cout << num << " ";}cout << endl;myList.pop_front();  // 删除列表头部的元素cout << "删除头部元素后列表元素: ";for (auto& num : myList){cout << num << " ";}cout << endl;
}
void Test10()
{list<int> myList = { 1, 2, 3 };list<int>::iterator it = myList.begin();it = myList.insert(it, 4);  // 这里迭代器 it 失效it = myList.insert(it, 5);  // 这里迭代器 it 失效for (auto& num : myList){cout << num << " ";}cout << endl;
}
void Test11()
{list<int> myList = { 1, 2, 3, 4, 5 };list<int>::iterator it = myList.begin();it = myList.erase(it);  // 迭代器 it 失效it = myList.erase(it);  // 迭代器 it 失效for (auto& num : myList){cout << num << " ";}cout << endl;
}
void Test12()
{list<int> list1 = { 1, 2, 3 };list<int> list2 = { 4, 5, 6 };cout << "交换之前:" << endl;for (auto& num : list1){cout << num << " ";}cout << endl;for (auto& num : list2){cout << num << " ";}cout << endl;list1.swap(list2);cout << "交换之前:" << endl;for (auto& num : list1){cout << num << " ";}cout << endl;for (auto& num : list2){cout << num << " ";}cout << endl;}
void Test13()
{list<int> list1 = { 1, 2, 3 };list<int> list2 = { 4, 5 };// 将 list2 的元素转移到 list1 中list1.splice(list1.end(), list2);for (auto num : list1) {cout << num << " ";}cout << endl;
}
void Test14()
{list<int> myList = { 1, 2, 2, 3, 2 };// 移除值为 2 的元素myList.remove(2);for (auto num : myList) {cout << num << " ";}cout << endl;
}
bool isEven(int num) {return num % 2 == 0;
}void Test15() {list<int> myList = { 1, 2, 3, 4, 5, 6 };// 移除满足偶数条件的元素myList.remove_if(isEven);for (auto num : myList) {cout << num << " ";}cout << endl;
}
void Test16()
{list<int> myList = { 1, 2, 2, 3, 3, 3 };// 移除相邻的重复元素myList.unique();for (auto num : myList) {cout << num << " ";}cout << endl;
}
void Test17()
{list<int> list1 = { 1, 3, 5 };list<int> list2 = { 2, 4, 6 };list1.sort();list2.sort();// 合并两个已排序的列表list1.merge(list2);for (auto num : list1) {cout << num << " ";}cout << endl;
}
void Test18() {list<int> myList = { 3, 1, 4, 1, 5, 9, 2, 6, 5 };// 对列表进行排序myList.sort();for (auto num : myList) {cout << num << " ";}cout << endl;
}
void Test19()
{list<int> l2 = { 1,2,4,5 };l2.reverse();//list中的reversereverse(l2.begin(), l2.end());//算法库中的reversefor (auto& num : l2){cout << num << " ";}
}
void TestListIterator1()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array + sizeof(array) / sizeof(array[0]));auto it = l.begin();while (it != l.end()){// erase()函数执行后,it所指向的节点已被删除,因此it无效,在下一次使用it时,必须先给//其赋值l.erase(it);++it;}
}
// 改正
void TestListIterator()
{int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };list<int> l(array, array + sizeof(array) / sizeof(array[0]));auto it = l.begin();while (it != l.end()){l.erase(it++); // it = l.erase(it);}
}int main()
{//Test1();//Test2();//Test4();//Test5();//Test6();//Test7();//Test8();//Test9();//Test10();//Test11();//Test12();//Test13();//Test14();//Test15();//Test16();//Test17();//Test18();Test19();
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/pingmian/90787.shtml
繁体地址,请注明出处:http://hk.pswp.cn/pingmian/90787.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

CIU32L051 DMA+Lwrb环形队列实现串口无阻塞性数据的收发 + 数据百分百不丢失的实现

1.Lwrb的介绍&#xff08;博主功能的实现是基于RT-thread系统实现&#xff09; Lwrb是由Tilen Majerle编写的一个线程安全的环形队列&#xff0c;通常与DMA配合实现数据的无阻塞性收发&#xff0c;同时&#xff0c;配合DMA的传输过半中断&#xff0c;传输完成中断&#xff0c;以…

【C++】C++ 的入门知识2

本篇文章主要讲解 C 的入门语法知识引用、inline 关键字与 nullptr 关键字。 目录 1 引用 1&#xff09; 引用的概念与定义 &#xff08;1&#xff09; 引用的概念 &#xff08;2&#xff09; 引用的定义 2&#xff09; 引用的特性 3&#xff09; 引用的使用场…

基于Kafka实现动态监听topic功能

生命无罪&#xff0c;健康万岁&#xff0c;我是laity。 我曾七次鄙视自己的灵魂&#xff1a; 第一次&#xff0c;当它本可进取时&#xff0c;却故作谦卑&#xff1b; 第二次&#xff0c;当它在空虚时&#xff0c;用爱欲来填充&#xff1b; 第三次&#xff0c;在困难和容易之间&…

机械学习初识--什么是机械学习--机械学习有什么重要算法

一、什么是机械学习机器学习&#xff08;Machine Learning&#xff09;是人工智能&#xff08;AI&#xff09;的一个重要分支&#xff0c;它使计算机能够通过数据自动学习规律、改进性能&#xff0c;并在没有明确编程的情况下完成特定任务。其核心思想是让机器从数据中 “学习”…

普通大学生大三这一年的想法

目录 大三期间的经历与反思 公益活动&#xff1a;社会责任感的体现 比赛&#xff1a;个人成长的助推器 培训与思想提升 大学教育的本质与人才培养 构建自我的道与未来规划 大学教育的未来与个人定位 结语 大三期间的经历与反思 大三&#xff0c;大学生活的分水岭&#…

Python——入门

目录 变量 变量类型 动态类型 注释 输出输入 运算符 算术运算符 关系运算符 逻辑运算符 赋值运算符 条件语句 循环语句 函数 函数作用域 函数嵌套调用 函数默认参数 关键字参数 列表 切片 列表遍历 新增元素 查找元素 删除元素 列表拼接 元组…

华为荣耀部分机型从鸿蒙降回EMUI的一种方法

一、准备说明 1、这里介绍使用华为手机助手、海外代理软件结合固件将部分华为荣耀手机鸿蒙系统降级回EMUI系 统的一种方式&#xff1b; 2、需要降级的手机需要再出厂时内置系统为EMUI&#xff0c;出厂时为鸿蒙系统的无法进行降级操作&#xff1b; 3、降级有风险&#xff0…

maven <dependencyManagement>标签的作用

作用 dependencyManagement标签的作用&#xff1a;在父工程pom文件中声明依赖&#xff0c;但不引入&#xff1b;在子工程中用到声明的依赖时&#xff0c;可以不加依赖的版本号&#xff0c;这样可以统一管理工程中用到的依赖版本。 示例 先创建一个项目 dependencyManagement-de…

JSON格式化与结构对比

说明 功能格式化json字符串为最简格式&#xff0c;并标识值类型&#xff1b;比对json字符串结构。第三方依赖fastjson: 用于解析json、判断json值类型&#xff1b;springframework自带的字符串判断&#xff0c;可以不依赖该方法&#xff0c;改为自行实现&#xff1b;slf4j: 用于…

编程与数学 03-002 计算机网络 03_物理层基础

编程与数学 03-002 计算机网络 03_物理层基础一、物理层的作用与任务&#xff08;一&#xff09;传输媒体的类型&#xff08;二&#xff09;信号的传输方式二、数据编码技术&#xff08;一&#xff09;数字数据的数字信号编码&#xff08;二&#xff09;模拟数据的数字信号编码…

c语言--文件操作

思维导图:1. 为什么使用文件&#xff1f; 如果没有文件&#xff0c;我们写的程序的数据是存储在电脑的内存中&#xff0c;如果程序退出&#xff0c;内存回收&#xff0c;数据就丢失了&#xff0c;等再次运⾏程序&#xff0c;是看不到上次程序的数据的&#xff0c;如果要将数据进…

SQL中的占位符、@Param注解和方法参数

代码中出现的多个 username 和 password 代表不同层面的变量&#xff0c;具体含义如下&#xff08;按执行顺序&#xff09;&#xff1a;### 1. Param("username") String username - 位置 &#xff1a;方法参数前的注解 - 作用 &#xff1a;- Param("username&q…

【SpringAI实战】FunctionCalling实现企业级自定义智能客服

一、前言 二、实现效果 三、代码实现 3.1 后端实现 3.2 前端实现 一、前言 Spring AI详解&#xff1a;【Spring AI详解】开启Java生态的智能应用开发新时代(附不同功能的Spring AI实战项目)-CSDN博客 二、实现效果 一个24小时在线的AI智能客服&#xff0c;可以给用户提供培…

kotlin基础【2】

变量类型var 和 val 的核心区别&#xff1a;关键字含义能否重新赋值类似概念&#xff08;Java&#xff09;varvariable&#xff08;可变变量&#xff09;可以普通变量&#xff08;无 final&#xff09;valvalue&#xff08;不可变变量&#xff09;不可以被 final 修饰的变量var…

【Spring AI】阿里云DashScope灵积模型

DashScope&#xff08;灵积模型&#xff09;是阿里云提供的大模型服务平台&#xff0c;集成了阿里自研的 通义千问&#xff08;Qwen&#xff09;系列大语言模型&#xff08;LLM&#xff09;以及多模态模型&#xff0c;为企业与开发者提供开箱即用的 AI 能力。官网地址 https://…

Rust Web框架性能对比与实战指南

Rust Actix Web Rust Web 框架的实用对比分析 以下是 Rust Web 框架的实用对比分析,涵盖主要框架(如 Actix-web、Rocket、Warp、Axum 等)的常见使用场景示例,按功能分类整理: 基础路由设置 Actix-web use actix_web::{get, App, HttpResponse, HttpServer, Responder}…

【解决vmware ubuntu不小心删boot分区,进不去系统】

如果仍然提示 Unable to locate package testdisk&#xff0c;有可能是源中不包含该工具&#xff08;LiveCD 使用的是“最小环境”&#xff09;。 &#x1fa9b; 解决方法&#xff1a;切换到国内完整软件源&#xff08;推荐&#xff09; 编辑 sources.list&#xff1a; sudo na…

04-netty基础-Reactor三种模型

1 基本概念Reactor模型是一种事件驱动&#xff08;Event-Driven&#xff09;的设计模式&#xff0c;主要用于高效处理高并发、I/O密集型场景&#xff08;如网络、服务器、分布式等&#xff09;。其核心思想就是集中管理事件&#xff0c;将I/O操作与业务逻辑解耦&#xff0c;避免…

踩坑无数!NFS服务从入门到放弃再到真香的血泪史

前言 说起NFS&#xff0c;我估计很多搞运维的兄弟都有一肚子话要说。这玩意儿吧&#xff0c;看起来简单&#xff0c;用起来坑多&#xff0c;但是真正搞明白了又觉得挺香的。 前几天有个朋友问我&#xff0c;说他们公司要搭建一个文件共享系统&#xff0c;问我推荐什么方案。我…

矩阵谱分解的证明及计算示例

1. 矩阵谱分解的条件矩阵的谱分解&#xff08;也称为特征分解&#xff09;是将一个矩阵分解为一系列由其特征向量和特征值构成的矩阵乘积的过程。进行谱分解的前提条件包括&#xff1a;<1.> 矩阵是可对角化的&#xff08;Diagonalizable&#xff09;&#xff0c;即矩阵存…