c++ list容器使用详解

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.hqwc.cn/news/194365.html

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

相关文章

AcWing 3. 完全背包问题 学习笔记

有 N&#xfffd; 种物品和一个容量是 V&#xfffd; 的背包&#xff0c;每种物品都有无限件可用。 第 i&#xfffd; 种物品的体积是 vi&#xfffd;&#xfffd;&#xff0c;价值是 wi&#xfffd;&#xfffd;。 求解将哪些物品装入背包&#xff0c;可使这些物品的总体积不…

iOS_折叠展开 FoldTextView

1. 显示效果 Test1&#xff1a;直接使用&#xff1a; Test2&#xff1a;在 cell 里使用&#xff1a; 2. 使用 2.1 直接使用 // 1.1 init view private lazy var mooFoldTextView: MOOFoldTextView {let view MOOFoldTextView(frame: .zero)view.backgroundColor .cyanvie…

模块化Common JS 和 ES Module

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

Python基础:错误和异常

在Python中的错误可&#xff08;至少&#xff09;被分为两种&#xff1a;语法错误和 异常&#xff0c;均是指在程序中发生的问题和意外情况。Python提供了异常处理机制&#xff0c;使程序能够更容易地应对这些问题。 1. 语法错误&#xff08;Syntax Error&#xff09; 语法错误…

YOLOv8优化策略:轻量级Backbone改进 | VanillaNet极简神经网络模型 | 华为诺亚2023

🚀🚀🚀本文改进:一种极简的神经网络模型 VanillaNet,支持vanillanet_5, vanillanet_6, vanillanet_7, vanillanet_8, vanillanet_9, vanillanet_10, vanillanet_11等版本 🚀🚀🚀YOLOv8改进专栏:http://t.csdnimg.cn/hGhVK 学姐带你学习YOLOv8,从入门到创新,…

ubuntu20.04在docker下运行ros-noetic进行开发

经常折腾虚拟机各双系统 &#xff0c; 想着不如把docker利用起来&#xff0c;下面算是一个初学者使用docker运行ros的记录&#xff1a; 1. 安装 使用官方安装脚本自动安装 curl -fsSL https://test.docker.com -o test-docker.shsudo sh test-docker.sh验证是否安装成功 doc…

命令执行相关函数及各类命令执行绕过技巧

相关函数 &#xff08;命令注入&#xff09; 命令执行的绕过

【LeetCode刷题日志】232.用栈实现队列

&#x1f388;个人主页&#xff1a;库库的里昂 &#x1f390;C/C领域新星创作者 &#x1f389;欢迎 &#x1f44d;点赞✍评论⭐收藏✨收录专栏&#xff1a;LeetCode 刷题日志&#x1f91d;希望作者的文章能对你有所帮助&#xff0c;有不足的地方请在评论区留言指正&#xff0c;…

公网访问全能知识库工具AFFINE,Notion的免费开源替代

文章目录 公网访问全能知识库工具AFFINE&#xff0c;Notion的免费开源替代品前言1. 使用Docker安装AFFINE2. 安装cpolar内网穿透工具3. 配置AFFINE公网访问地址4. 实现公网远程访问AFFINE 公网访问全能知识库工具AFFINE&#xff0c;Notion的免费开源替代品 前言 AFFiNE 是一个…

穷举法、回溯法、分支界限法解决旅行商(TSP)问题

文章目录 一、问题描述二、穷举法解决2.1 介绍2.2 代码 三、回溯法解决四、分支界限法4.1 介绍4.2 代码 一、问题描述 有一个旅行商由某城市出发&#xff0c;经过所有给定的 n n n 个城市后&#xff0c;再回到出发的城市。除了出发的城市外&#xff0c;其它城市只经过一回。这…

RepVgg: 网络结构重参化

CVPR2021 截至目前1004引 论文连接 代码连接 文章提出的问题 大多数的研究者追求的是设计一个好的网络结构,这种“好”体现在网络具有复杂的网络设计,这种网络虽然比简单的网络收获了更加高的准确率,但是网络结构中的大量并行分支,导致模型的难以应用和自定义,主要体现…

【神印王座】月夜大尺度诱惑,皓晨潜入月魔宫,枫秀降临男扮女装

Hello,小伙伴们&#xff0c;我是拾荒君。 为了能安全回到联盟&#xff0c;龙皓晨决定让月夜商队护送他们&#xff0c;这也是他们目前处境更快更安全回到人类境地的方法。于是&#xff0c;龙皓晨只身一人去寻找月夜&#xff0c;此次执行的任务完全超出龙皓晨的掌握之外&#xf…