《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
英文地址,請注明出處:http://en.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;即矩陣存…