【C++】list容器

1.list基本概念

在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述
在這里插入圖片描述

2.list構造函數

在這里插入圖片描述
在這里插入圖片描述

#include <iostream>
using namespace std;#include<list>
//鏈表list容器構造函數//輸出list鏈表
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl; //換行
}void test01()
{//創建list容器list<int>L1; //默認構造//添加數據L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍歷容器printList(L1);//區間方式構造list<int>L2(L1.begin(), L1.end());printList(L2);//拷貝構造list<int>L3(L2);printList(L3);//n個elemlist<int>L4(10,1000);printList(L4);
}
int main() 
{ test01();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

3.list賦值和交換

在這里插入圖片描述
在這里插入圖片描述

#include <iostream>
using namespace std;//list容器的賦值和交互
#include<list>
//遍歷list容器
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}//賦值
void test01() 
{//創建list容器list<int>L1;//給L1賦值L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//遍歷printList(L1);list<int>L2;L2 = L1; //operator= 賦值printList(L2);list<int>L3;L3.assign(L2.begin(), L2.end()); //按區間賦值printList(L3);list<int>L4;L4.assign(10, 1000); //n個elemprintList(L4);
}
//交換
void test02()
{//創建list容器list<int>L1;//給L1賦值L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);list<int>L2;//給L2賦值L2.assign(10, 1000); //n個elemcout << "交換前:" << endl;printList(L1);printList(L2);L1.swap(L2); //交換cout << "交換后:" << endl;printList(L1);printList(L2);
}
int main() 
{ test01();cout << endl;test02();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

4.list大小操作

在這里插入圖片描述
在這里插入圖片描述

#include <iostream>
using namespace std;//list容器大小操作
#include <list>
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);printList(L1);//判斷L1是否為空if (L1.empty()){cout << "L1為空" << endl;}else{cout << "L1不為空" << endl;cout << "L1的大小為:" << L1.size() << endl;}//重新指定大小L1.resize(10);printList(L1);//默認值定為了1000L1.resize(15, 1000);printList(L1);L1.resize(5); //把多余的值刪除printList(L1);
}int main() 
{ test01();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

5.list插入和刪除

在這里插入圖片描述
在這里插入圖片描述

#include <iostream>
using namespace std;//list容器的插入和刪除
#include<list>
void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl;
}void test01()
{list<int>L1;//尾插L1.push_back(10);L1.push_back(20);L1.push_back(30);L1.push_back(40);//頭插L1.push_front(10);L1.push_front(20);L1.push_front(30);//30 20 10 10 20 30 40printList(L1);//尾刪L1.pop_back();//30 20 10 10 20 30printList(L1);//頭刪L1.pop_front();//20 10 10 20 30printList(L1);//insert插入L1.insert(L1.begin(), 1000);//1000 20 10 10 20 30printList(L1);//利用it來插入指定位置list<int>::iterator it = L1.begin();L1.insert(++it, 5, 20000);//1000 20000 20000 20000 20000 20000 20 10 10 20 30printList(L1);//刪除it = L1.begin();L1.erase(++it);//1000 20000 20000 20000 20000 20 10 10 20 30printList(L1);//移除L1.remove(20000); //移除所有值為20000的數據//1000 20 10 10 20 30printList(L1);//清空//L1.erase(L1.begin(), L1.end()); 這種區間刪除的方法也可以L1.clear();printList(L1);
}int main() 
{ test01();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

6.list數據存取

在這里插入圖片描述
在這里插入圖片描述

#include <iostream>
using namespace std;//list容器的數據存取
#include<list>
void test01()
{list<int>L1;L1.push_back(10);L1.push_back(20);L1.push_back(30);//L1[0]; //錯誤 不可以用[]訪問list容器中的元素//L1.at(0); //錯誤 不可以用.at()方式訪問list容器中的元素//原因是list本質鏈表,不是用連續性空間存儲數據,迭代器也是不支持隨機訪問的cout << "第一個元素為:" << L1.front() << endl;cout << "最后一個元素為:" << L1.back() << endl;//list容器的迭代器是雙向迭代器,不支持隨機訪問的list<int>::iterator it = L1.begin();it++;cout << *it << endl; //輸出第二個元素:20it--;cout << *it << endl; //輸出第一個元素:10//it = it + 1; //錯誤 不可以跳躍訪問,即使是+1
}
int main()
{ test01();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

7.list反轉和排序

在這里插入圖片描述
在這里插入圖片描述

#include <iostream>
using namespace std;//list容器反轉和排序
#include <list>void printList(const list<int>& L)
{for (list<int>::const_iterator it = L.begin(); it != L.end(); it++){cout << *it << " ";}cout << endl; //換行
}bool myCompare(int v1, int v2)
{// 降序 讓第一個數 > 第二個數return v1 > v2;
}void test01()
{list<int>L;L.push_back(10);L.push_back(30);L.push_back(20);L.push_back(50);L.push_back(40);cout << "反轉前:" << endl;//10 30 20 50 40printList(L);//反轉L.reverse();cout << "反轉后:" << endl;//40 50 20 30 10printList(L);//排序// sort(L.begin(), L.end()); 錯誤// 所有不支持隨機訪問迭代器的容器,不可以用標準算法algorithm// 不支持隨機訪問迭代器的容器,內部會提供對應一些算法//默認升序L.sort();  // 默認排序規則 從小到大 升序cout << "排序后:" << endl;printList(L);//降序L.sort(myCompare); // 降序,指定規則,從大到小printList(L);
}
int main()
{ test01();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

8.排序案例

在這里插入圖片描述

在這里插入圖片描述

#include <iostream>
using namespace std;//排序案例
#include <list>
#include <string>//Person類 三國人物
class Person
{
public:Person(string name, int age, float height){this->m_Name = name;this->m_Age = age;this->m_Height = height;}string m_Name;int m_Age;float m_Height;
};  //忘記加 ; 導致一直報錯!! 相對于類的聲明 例如: int a;//遍歷list
void printList(const list<Person>& L)
{for (list<Person>::const_iterator it = L.begin(); it != L.end(); it++){cout << "姓名:" << (*it).m_Name << "\t年齡:"<< (*it).m_Age << "\t身高:" << it->m_Height << endl;}
}//制定排序規則
bool comparePerson(Person& p1, Person& p2)
{//按照年齡 升序if (p1.m_Age == p2.m_Age){//年齡相同 按照身高降序return p1.m_Height > p2.m_Height;}else{return p1.m_Age < p2.m_Age;}
}void test01()
{list<Person>L1; // 創建容器//準備數據Person p1("劉備", 35, 175);Person p2("曹操", 45, 180);Person p3("孫權", 40, 170);Person p4("趙云", 25, 190);Person p5("張飛", 35, 160);Person p6("關羽", 35, 200);//插入數據L1.push_back(p1);L1.push_back(p2);L1.push_back(p3);L1.push_back(p4);L1.push_back(p5);L1.push_back(p6);cout << "排序前:" << endl;printList(L1);cout << "-------------------------" << endl;cout << "排序后:" << endl;//排序:L1.sort(comparePerson);printList(L1);
}int main()
{ test01();//**************************************system("pause");return 0;
} 

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/40939.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/40939.shtml
英文地址,請注明出處:http://en.pswp.cn/news/40939.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

STM32入門學習之定時器PWM輸出

1.脈沖寬度調制PWM(Pulse Width Modulation)是利用微處理器的數字輸出來對模擬電路進行控制的一種非常有效的技術。PWM可以理解為高低電平的占空比&#xff0c;即輸出高電平時間與低電平時間的比值。PWM的應用是否廣泛&#xff0c;比如在步進電機的控制中&#xff0c;可以通過P…

【MySQL系列】-回表、覆蓋索引真的懂嗎

【MySQL系列】-回表、覆蓋索引真的懂嗎 文章目錄 【MySQL系列】-回表、覆蓋索引真的懂嗎一、MYSQL索引結構1.1 索引的概念1.2 索引的特點1.3 索引的優點1.4 索引的缺點 二、B-Tree與BTree2.1 B-Tree2.2 BTree2.3 B-Tree 與BTree樹的區別2.4 那么為什么InnoDB的主鍵最好要搞成有…

記一次 .NET 某外貿ERP 內存暴漲分析

一&#xff1a;背景 1. 講故事 上周有位朋友找到我&#xff0c;說他的 API 被多次調用后出現了內存暴漲&#xff0c;讓我幫忙看下是怎么回事&#xff1f;看樣子是有些擔心&#xff0c;但也不是特別擔心&#xff0c;那既然找到我&#xff0c;就給他分析一下吧。 二&#xff1…

【軟件測試】接口測試工具APIpost

說實話&#xff0c;了解APIpost是因為&#xff0c;我的所有接口相關的文章下&#xff0c;都有該APIpost水軍的評論&#xff0c;無非就是APIpost是中文版的postman&#xff0c;有多么多么好用&#xff0c;雖然咱也還不是什么啥網紅&#xff0c;但是不知會一聲就亂在評論區打廣告…

【力扣每日一題】2023.8.14 合并二叉樹

目錄 題目&#xff1a; 示例&#xff1a; 分析&#xff1a; 代碼&#xff1a; 題目&#xff1a; 示例&#xff1a; 分析&#xff1a; 給我們合并兩棵二叉樹&#xff0c;合并的方式就是把對應位置的節點的值相加&#xff0c;最后把合并后的二叉樹的根節點返回出去。 這類二…

You have docker-compose v1 installed, but we require Docker Compose v2.

curl -SL https://github.com/docker/compose/releases/download/v2.2.3/docker-compose-linux-x86_64 -o /usr/local/bin/docker-compose chmod x /usr/local/bin/docker-compose docker-compose --version

一文看盡R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD詳解

一文看盡R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD詳解 以下六篇文章總結詳細&#xff1a; 1. 一文讀懂目標檢測&#xff1a;R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD 2. 【深度學習】R-CNN 論文解讀及個人理解 3、R-CNN論文詳解 4、一文讀懂Faster RCNN 5、學一百遍都…

JAVA基礎知識(六)——異常處理

異常 一、異常概述與異常體系結構二、常見異常三、異常處理機制一&#xff1a;try-catch-finally四、異常處理機制二&#xff1a;throws五、手動拋出異常&#xff1a;throw六、用戶自定義異常類七、開發中如何選擇使用try-catch-finally還是使用throws八、如何看待代碼中的編譯…

goland插件推薦Rider UI Theme Pack

推薦一個goland配色插件Rider UI Theme Pack&#xff0c;里面自帶visual assist配色&#xff0c;配色截圖如下&#xff1a; 直接在plugins里面進行搜索或者在插件home page下載后進行安裝&#xff0c; 然后按照下圖進行設置即可。 此插件還適用于Jetbrains旗下的Clion和Pycharm…

WX1860- ngbe-1.2.5 xdp程序在路由模式下,使用iperf工具測試數據包不轉發,用jmeter可以

本地驗證時重定向iperf包有出現calltrace錯誤&#xff0c;經推斷&#xff0c;系統PAGE_SIZE<8k時可能出現&#xff08;getconf PAGE_SIZE指令可查看&#xff09;&#xff0c;按下圖將ngbe_main.c的2350行ngbe_rx_bufsz改為ngbe_rx_pg_size可修復。其次&#xff0c;需要將加載…

鴻蒙3.1 基于Token的訪問控制

介紹 代碼路徑:security_access_token: ATM(AccessTokenManager)是OpenHarmony上基于AccessToken構建的統一的應用權限管理能力。 ATM(AccessTokenManager)是OpenHarmony上基于AccessToken構建的統一的應用權限管理能力。 應用的Accesstoken信息主要包括應用身份標識APPID、…

什么是游戲出海運營?

游戲出海運營&#xff0c;也稱為游戲海外運營&#xff0c;是指將原本面向國內市場的游戲產品拓展到國際市場&#xff0c;以在海外地區推廣、發行、運營游戲的過程。這涵蓋了從市場調研、產品適應性優化、本地化翻譯、推廣營銷、社區互動到客戶支持等一系列策略和活動&#xff0…

阿里云對象存儲服務OSS

1、引依賴 <dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId><version>3.15.1</version> </dependency> <dependency><groupId>javax.xml.bind</groupId><artifa…

Mocha and Red and Blue

一、題目 題面翻譯 給定長為 n n n 的僅由 R \texttt{R} R、 B \texttt{B} B、 ? \texttt{?} ? 組成的字符串 S S S&#xff0c;請你在 ? \texttt{?} ? 處填入 R \texttt{R} R 或 B \texttt{B} B&#xff0c;使得相鄰位置字符相同的數量最少。 譯者 ajthreac 題…

Hadoop HA集群兩個NameNode都是standby或者主NameNode是standby,從NameNode是active的情況集錦

文章目錄 背景架構HDFS HA配置錯誤原因解決方案方案一方案二方案三&#xff08;首先查看自己各參數文件是否配置出錯&#xff09; 后記補充failovertransitionToActive 常用端口號及配置文件常用端口號hadoop3.xhadoop2.x 常用配置文件 這里說一下配置Hadoop HA集群可能出現的兩…

Linux多線程【初識線程】

?個人主頁&#xff1a; 北 海 &#x1f389;所屬專欄&#xff1a; Linux學習之旅 &#x1f383;操作環境&#xff1a; CentOS 7.6 阿里云遠程服務器 文章目錄 &#x1f307;前言&#x1f3d9;?正文1、什么是線程&#xff1f;1.1、基本概念1.2、線程理解1.3、進程與線程的關系…

分布式事務與解決方案

一、什么是分布式事務 首先我們知道本地事務是指事務方法中的操作只依賴本地數據庫&#xff0c;可保證事務的ACID特性。而在分布式系統中&#xff0c;一個應用系統被拆分為多個可獨立部署的微服務&#xff0c;在一個微服務的事務方法中&#xff0c;除了依賴本地數據庫外&#…

【深入理解ES6】塊級作用域綁定

1. var聲明及變量提升機制 提升&#xff08;Hoisting&#xff09;機制&#xff1a;通過關鍵字var聲明的變量&#xff0c;都會被當成在當前作用域頂部生命的變量。 function getValue(condition){if(condition){var value "blue";console.log(value);}else{// 此處…

代碼隨想錄算法訓練營第三十六天 | 435. 無重疊區間,763.劃分字母區間,56. 合并區間

代碼隨想錄算法訓練營第三十六天 | 435. 無重疊區間&#xff0c;763.劃分字母區間&#xff0c;56. 合并區間 435. 無重疊區間:eyes:題目總結:eyes: 763.劃分字母區間:eyes:題目總結:eyes: 56. 合并區間:eyes:題目總結:eyes: 435. 無重疊區間 題目鏈接 視頻講解 給定一個區間的…

并發編程系列-Semaphore

Semaphore&#xff0c;如今通常被翻譯為"信號量"&#xff0c;過去也曾被翻譯為"信號燈"&#xff0c;因為類似于現實生活中的紅綠燈&#xff0c;車輛是否能通行取決于是否是綠燈。同樣&#xff0c;在編程世界中&#xff0c;線程是否能執行取決于信號量是否允…