c++ list容器使用详解

news/2025/3/28 15:22:58/

list容器概念

list是一个双向链表容器,可高效地进行插入删除元素。

List 特点:

  • list不可以随机存取元素,所以不支持at.(position)函数与[]操作符。可以对其迭代器执行++,但是不能这样操作迭代器:it+3
  • 使用时包含 #include <list>  

list对象的构造函数

list同样采用模板类实现,对象的默认构造形式:list<T> listT;  如:

  1. list<int> lstInt;            //定义一个存放int的list容器。
  2. list<float> lstFloat;        //定义一个存放float的list容器。
  3. list<string> lstString;       //定义一个存放string的list容器。

注意:尖括号内还可以设置指针类型或自定义类型

list对象的带参构造函数

方式一:list(beg,end);     //将[beg, end)区间中的元素拷贝给本身。

方式二:list(n,elem);      //构造函数将n个elem拷贝给本身。

方式三:list(const list &lst); //拷贝构造函数。

list<int> lstInt1;lstInt1.push_back(1);lstInt1.push_back(2);lstInt1.push_back(3);list<int> lstInt2(lstInt1.begin(),lstInt1.end());		//1 2 3 list<int> lstInt3(5,8);							//8 8 8 8 8list<int> lstInt4(lstIntA);						    //1 2 3 

list头尾的添加移除操作

list<int> lstInt;lstInt.push_back(1);lstInt.push_back(2);lstInt.push_back(3);lstInt.push_back(4);lstInt.push_back(5);lstInt.pop_front();lstInt.pop_front();lstInt.push_front(11);lstInt.push_front(12);lstInt.pop_back();lstInt.pop_back();
// lstInt    {12, 11, 3}

list数据的读取

  1. list.front();   //返回第一个元素。
  2. list.back();  //返回最后一个元素。
list<int> lstInt;lstInt.push_back(1);lstInt.push_back(2);lstInt.push_back(3);lstInt.push_back(4);lstInt.push_back(5);int iFront = lstInt.front();	//1int iBack = lstInt.back();		//5lstInt.front() = 11;			//11lstInt.back() = 19;			//19

list与迭代器

list<int> lstInt;lstInt.push_back(1);lstInt.push_back(3);lstInt.push_back(5);lstInt.push_back(7);lstInt.push_back(9);for (list<int>::iterator it=lstInt.begin(); it!=lstInt.end(); ++it){cout << *it;cout << " ";}for (list<int>::reverse_iterator rit=lstInt.rbegin(); rit!=lstInt.rend(); ++rit){cout << *rit;cout << " ";}

list的赋值

llist<int> lstIntA,lstIntB,lstIntC,lstIntD;lstIntA.push_back(1);lstIntA.push_back(3);lstIntA.push_back(5);lstIntA.push_back(7);lstIntA.push_back(9);lstIntB.assign(lstIntA.begin(),lstIntA.end());		//1 3 5 7 9lstIntB.assign(++lstIntA.begin(),--lstIntA.end());		//3 5 7lstIntC.assign(5,8);							//8 8 8 8 8lstIntD = lstIntA;							//1 3 5 7 9lstIntC.swap(lstIntD);						//互换

list的大小

list<int> lstIntA;lstIntA.push_back(1);lstIntA.push_back(2);lstIntA.push_back(3);if (!lstIntA.empty()){int iSize = lstIntA.size();		//3lstIntA.resize(5);			//1 2 3 0 0lstIntA.resize(7,1);			//1 2 3 0 0 1 1lstIntA.resize(5);			//1 2 3 0 0}

list的插入

list<int> listA;list<int> listB;listA.push_back(1);listA.push_back(2);listA.push_back(3);listA.push_back(4);listA.push_back(5);listB.push_back(11);listB.push_back(12);listB.push_back(13);listB.push_back(14);listA.insert(listA.begin(), -1);		//{-1, 1, 2, 3, 4, 5}listA.insert( ++listA.begin(), 2, -2);	//{-1, -2, -2, 1, 2, 3, 4, 5}listA.insert(listA.begin() , listB.begin() , listB.end());	//{11, 12, 13, 14, -1, -2, -2, 1, 2, 3, 4, 5}for(list<int>::iterator it = listA.begin(); it!=listA.end(); it++){cout<< *it<<endl;}

list的删除

// demo 15-32
#include <list>
#include <vector>
#include <iostream>using namespace std;int main(void){//list 删除元素list<int> listA;listA.push_back(1);listA.push_back(2);listA.push_back(3);listA.push_back(4);listA.push_back(5);//erase 的用法list<int>::iterator itBegin=listA.begin();++ itBegin;list<int>::iterator itEnd=listA.begin();++ itEnd;++ itEnd;++ itEnd;listA.erase(itBegin,itEnd);//此时容器lstInt包含按顺序的1, 4, 5三个元素。listA.erase(listA.begin());//此时容器lstInt包含按顺序的4, 5三个元素。listA.push_back(4); // 4, 5, 4listA.insert(listA.end(), 5, 4);  //4, 5, 4, 4, 4, 4, 4, 4/*remove 删除元素*///方式一  直接调用remove 方法//listA.remove(4);//方式二   遍历然后逐个删除for(list<int>::iterator it=listA.begin(); it!=listA.end(); ){if(*it == 4){it =listA.erase(it); //相当于执行了++}else {it++;}}for (list<int>::iterator it=listA.begin(); it!=listA.end(); ++it){cout << *it;cout << " ";}system("pause");return 0;
}

list的反向排列

  1. list.reverse();     //反转链表,比如list包含1, 2, 3, 4, 5五个元素,运行此方

        法后,list就包含5, 4, 3, 2, 1元素。

list<int> listA;listA.push_back(1);listA.push_back(2);listA.push_back(3);listA.push_back(4);listA.push_back(5);listA.reverse();			//5, 4, 3, 2, 1


http://www.ppmy.cn/news/1227413.html

相关文章

Docker中快速安装RabbitMQ

文章目录 前言一、安装Docker二、安装RabbitMQ无脑命令行运行 总结 前言 在Ubuntu中的Docker容器中快速安装RabbitMQ&#xff0c;亲测有效&#xff0c;不废话&#xff0c;上操作。 一、安装Docker 直接按照Docker官方教程操作&#xff1a;官方安装教程 点进官网&#xff0c;往…

【佳学基因检测】在LARAVEL中如何使用和设置路由组

【佳学基因检测】在LARAVEL中如何使用和设置路由组 在设计面向基因检测客户的动态网页和API中&#xff0c;基因检测人工智能常常使用LARAVEL网站框架。Laravel中&#xff0c;路由组&#xff08;Route Groups&#xff09;是一种用于组织和管理路由的方法。通过使用路由组&#…

Redis 9 数据库

4 设置键的生存时间或过期时间 通过EXPIRE命令或者PEXPIRE命令&#xff0c;客户端可以以秒或者毫秒精度为数据库中的某个键设置生存时间&#xff08;TimeToLive&#xff0c;TTL&#xff09;&#xff0c;在经过指定的秒数或者毫秒数之后&#xff0c;服务器就会自动删除生存时间…

模块化Common JS 和 ES Module

目录 历程 1.几个函数&#xff1a;全局变量的污染&#xff0c;模块间没有联系 2.对象&#xff1a;暴露成员&#xff0c;外部可修改 3.立即执行函数&#xff1a;闭包实现模块私有作用域 common JS module和Module 过程 模块依赖&#xff1a;深度优先遍历、父 -> 子 -…

虚幻C++ day5

角色状态的常见机制 创建角色状态设置到UI上 在MainPlayer.h中新建血量&#xff0c;最大血量&#xff0c;耐力&#xff0c;最大耐力&#xff0c;金币变量&#xff0c;作为角色的状态 //主角状态UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category "Playe Stats&…

音视频项目—基于FFmpeg和SDL的音视频播放器解析(九)

介绍 在本系列&#xff0c;我打算花大篇幅讲解我的 gitee 项目音视频播放器&#xff0c;在这个项目&#xff0c;您可以学到音视频解封装&#xff0c;解码&#xff0c;SDL渲染相关的知识。您对源代码感兴趣的话&#xff0c;请查看基于FFmpeg和SDL的音视频播放器 如果您不理解本…

ffmpeg知识点整理

使用FFmepg进行视频转码、视频格式转换、图片提取等&#xff01;_ffmepg -c:v-CSDN博客 中文文档&#xff1a; ffmpeg 中文手册 (beandrewang.github.io) 笔记&#xff1a; 通用规则是&#xff0c;所有选项作用于其后边的第一个文件。因此&#xff0c;顺序是非常重要的&…

AUTODL云服务器使用大致步骤(适合本人版)

(一)在官网上创建一个服务器 (二)远程连接指令&#xff1a; 改为&#xff1a; (三)连接后&#xff0c;可在中进行代码运行 输入一些指令 python ......