当前位置: 首页 > news >正文

【C++】———list容器

前言

1.list容器简单来说其实就是之前的链表结构。

2.这里的list用的是双向带头结点的循环链表。

目录

前言

一  构造函数

1.1   list ();

1.2    list (size_type n, const value_type& val = value_type() );

1.3   list (InputIterator first, InputIterator last );

1.4   list (const list& x);

二  析构函数 

~list();

三  赋值运算符重载

list& operator= (const list& x);

 四  迭代器

 4.1  正向迭代器

4.2  反向迭代器

​ 五 容量函数

5.1  bool empty() const;

5.2  size_type size() const;

六  修改器

6.1  assign()

 6.2  插入数据和删除数据

5.insert ()

6.erase()

6.3  void resize (size_type n, value_type val = value_type());

6.4 void clear();

 七  操作

7.1  splice()

 7.2 unique()

总结


一  构造函数

1.1   list ();

括号里面是一个适配器。

 空容器构造函数(默认构造函数)

 构造一个没有元素的空容器。 

1.2    list (size_type n, const value_type& val = value_type() );

填充构造函数

构造一个包含n个元素的容器。每个元素都是val的一个副本。

1.3   list (InputIterator first, InputIterator last );

里面的InputIterator代表的是迭代器的类型。

范围构造函数

构造一个具有与范围[first,last)一样多元素的容器,其中每个元素都按照相同的顺序由该范围内的相应元素构造。

1.4   list (const list& x);

拷贝构造函数

按照相同顺序构造一个容器,其中包含x中每个元素的副本。

 测试案例:

#define _CRT_SECURE_NO_WARNINGS 1
// constructing lists
#include <iostream>
#include <list>
using namespace std;
int main()
{std::list<int> first;                                // 一个空列表std::list<int> second(4, 100);                       // 4个100的值std::list<int> third(second.begin(), second.end());  // 迭代器用second的值初始化thirdstd::list<int> fourth(third);                       // third的一个拷贝// 这里也可以用数组去迭代初始化int myints[] = { 16,2,77,29 };std::list<int> fifth(myints, myints + sizeof(myints) / sizeof(int));cout << "first:";for (std::list<int>::iterator it = first.begin(); it != first.end(); it++)std::cout << *it << ' ';cout << endl;cout << "second:";for (std::list<int>::iterator it = second.begin(); it != second.end(); it++)std::cout << *it << ' ';cout << endl;cout << "third:";for (std::list<int>::iterator it = third.begin(); it != third.end(); it++)std::cout << *it << ' ';cout << endl;cout << "fourth:";for (std::list<int>::iterator it = fourth.begin(); it != fourth.end(); it++)std::cout << *it << ' ';cout << endl;std::cout << "fifth: ";for (std::list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)std::cout << *it << ' ';std::cout << '\n';return 0;
}

二  析构函数 

~list();

把所有容器元素都销毁,并且把分配器的空间释放。

三  赋值运算符重载

list& operator= (const list& x);

测试用例:

#define _CRT_SECURE_NO_WARNINGS 1
// assignment operator with lists
#include <iostream>
#include <list>
using namespace std;
int main()
{list<int> first(3);      // 初始化为3list<int> second(5);     // 初始化为5second = first;//赋值first = list<int>();//把一个匿名对象赋值给firstcout << "Size of first: " << int(first.size()) << endl;cout << "Size of second: " << int(second.size()) << endl;return 0;
}

​ 

 四  迭代器

迭代器分为正向迭代器和反向迭代器

 4.1  正向迭代器

iterator begin()   

iterator end()

测试用例

#define _CRT_SECURE_NO_WARNINGS 1
// list::begin
#include <iostream>
#include <list>
using namespace std;
int main()
{int myints[] = { 75,23,65,42,13 };list<int> mylist(myints, myints + 5);std::cout << "mylist contains:";for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)cout << ' ' << *it;cout <<endl;return 0;
}

4.2  反向迭代器

reverse_iterator rbegin();

reverse_iterator rend();

测试用例

#define _CRT_SECURE_NO_WARNINGS 1
// list::rbegin/rend
#include <iostream>
#include <list>
using namespace std;
int main()
{list<int> mylist;for (int i = 1; i <= 5; i++){mylist.push_back(i);}cout << "mylist backwards:";for (list<int>::reverse_iterator rit = mylist.rbegin(); rit != mylist.rend(); ++rit)cout << ' ' << *rit;cout << endl;return 0;
}

​ 
五 容量函数

5.1  bool empty() const;
// list::empty
#include <iostream>
#include <list>int main()
{std::list<int> mylist;int sum(0);for (int i = 1; i <= 10; ++i) mylist.push_back(i);while (!mylist.empty()){sum += mylist.front();mylist.pop_front();}std::cout << "total: " << sum << '\n';return 0;
}

为了方便测试,所以这里用了没有说明的函数。

5.2  size_type size() const;
// list::size
#include <iostream>
#include <list>int main()
{std::list<int> myints;std::cout << "0. size: " << myints.size() << '\n';for (int i = 0; i < 10; i++) myints.push_back(i);std::cout << "1. size: " << myints.size() << '\n';myints.insert(myints.begin(), 10, 100);std::cout << "2. size: " << myints.size() << '\n';myints.pop_back();std::cout << "3. size: " << myints.size() << '\n';return 0;
}

 

六  修改器

6.1  assign()

替换内容,并相应的修改大小

void assign (InputIterator first, InputIterator last);

void assign (size_type n, const value_type& val);

/ list::assign
#include <iostream>
#include <list>int main ()
{std::list<int> first;std::list<int> second;first.assign (7,100);                    //设置7个100的值second.assign (first.begin(),first.end());//用这7个值去替代second里面的内容int myints[]={1776,7,4};first.assign (myints,myints+3);            //用数组里面的内容去替代std::cout << "Size of first: " << int (first.size()) << '\n';std::cout << "Size of second: " << int (second.size()) << '\n';return 0;
}

 6.2  插入数据和删除数据

1.void push_front (const value_type& val);//头插

2.void pop_front();//头删

3.void push_back (const value_type& val);//尾插

4.void pop_back();//尾删

这些操作函数在上面的测试中都有演示


#include <iostream>
#include <list>int main()
{std::list<int> mylist;int sum(0);mylist.push_back(100);//尾插mylist.push_back(200);mylist.push_back(300);mylist.push_front(400);//头插mylist.pop_front();while (!mylist.empty()){sum += mylist.back();mylist.pop_back();//尾删}std::cout << "The elements of mylist summed " << sum << '\n';return 0;
}

5.insert ()

 iterator insert (iterator position, const value_type& val);//插入一个数据

void insert (iterator position, size_type n, const value_type& val);//插入n个数据

// inserting into a list
#include <iostream>
#include <list>
#include <vector>int main()
{std::list<int> mylist;std::list<int>::iterator it;for (int i = 1; i <= 5; ++i) mylist.push_back(i); // 1 2 3 4 5it = mylist.begin();++it;       mylist.insert(it, 10);                        // 1 10 2 3 4 5mylist.insert(it, 2, 20);                      // 1 10 20 20 2 3 4 5--it;       // it 指向第二个20            ^std::vector<int> myvector(2, 30);mylist.insert(it, myvector.begin(), myvector.end());// 1 10 20 30 30 20 2 3 4 5//               ^std::cout << "mylist contains:";for (it = mylist.begin(); it != mylist.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

 

6.erase()

iterator erase (iterator position);//删除一个数据

iterator erase (iterator first, iterator last);//删除迭代器区间的数据

// erasing from list
#include <iostream>
#include <list>int main()
{std::list<int> mylist;std::list<int>::iterator it1, it2;// set some values:for (int i = 1; i < 10; ++i) mylist.push_back(i * 10);// 10 20 30 40 50 60 70 80 90it1 = it2 = mylist.begin(); // ^^advance(it2, 6);            // ^                 ^++it1;                      //    ^              ^it1 = mylist.erase(it1);   // 10 30 40 50 60 70 80 90//    ^           ^it2 = mylist.erase(it2);   // 10 30 40 50 60 80 90//    ^           ^++it1;                      //       ^        ^--it2;                      //       ^     ^mylist.erase(it1, it2);     // 10 30 60 80 90//        ^std::cout << "mylist contains:";for (it1 = mylist.begin(); it1 != mylist.end(); ++it1)std::cout << ' ' << *it1;std::cout << '\n';return 0;
}

这里的advance起到一个推动迭代器的功能,

这里删除元素的时候需要用一个变量来接收是为了防止迭代器失效的问题。

6.3  void resize (size_type n, value_type val = value_type());

1.如果n<当前容量大小,那么会把容量缩到n,并且删除多余的元素

2.如果n>当前容量,会把容量扩到n,多余的元素会以val值来填充

// resizing list
#include <iostream>
#include <list>int main()
{std::list<int> mylist;// set some initial content:for (int i = 1; i < 10; ++i) mylist.push_back(i);mylist.resize(5);mylist.resize(8, 100);mylist.resize(12);std::cout << "mylist contains:";for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

 

6.4 void clear();
// clearing lists
#include <iostream>
#include <list>int main()
{std::list<int> mylist;std::list<int>::iterator it;mylist.push_back(100);mylist.push_back(200);mylist.push_back(300);std::cout << "mylist contains:";for (it = mylist.begin(); it != mylist.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';mylist.clear();//清除mylist.push_back(1101);mylist.push_back(2202);std::cout << "mylist contains:";for (it = mylist.begin(); it != mylist.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

 七  操作

7.1  splice()

移动后,原容器的元素会被删除掉

void splice (iterator pos, list& x);//将元素从x转移到容器中,并将它们插入到指定位置。

void splice (iterator pos, list& x, iterator i);//移动指定的元素

void splice (iterator pos, list& x, iterator first, iterator last);//移动迭代区间的元素到容器里面

// splicing lists
#include <iostream>
#include <list>int main()
{std::list<int> mylist1, mylist2;std::list<int>::iterator it;for (int i = 1; i <= 4; ++i)mylist1.push_back(i);      // mylist1: 1 2 3 4for (int i = 1; i <= 3; ++i)mylist2.push_back(i * 10);   // mylist2: 10 20 30it = mylist1.begin();++it;                         // points to 2mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4// mylist2 (empty)// it 依然指向2mylist2.splice(mylist2.begin(), mylist1, it);// mylist1: 1 10 20 30 3 4// mylist2: 2// it现在已经失效了it = mylist1.begin();//重新赋值std::advance(it, 3);           // it 指向30mylist1.splice(mylist1.begin(), mylist1, it, mylist1.end());// mylist1: 30 3 4 1 10 20std::cout << "mylist1 contains:";for (it = mylist1.begin(); it != mylist1.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';std::cout << "mylist2 contains:";for (it = mylist2.begin(); it != mylist2.end(); ++it)std::cout << ' ' << *it;std::cout << '\n';return 0;
}

 7.2 unique()

相当于是一个去重的函数

void unique();

int main()
{std::list<int> mylist1;std::list<int>::iterator it;mylist1.push_back(1);mylist1.push_back(1);mylist1.push_back(2);mylist1.push_back(2);mylist1.push_back(4);mylist1.push_back(4);mylist1.push_back(5);for (it = mylist1.begin(); it != mylist1.end(); it++){std::cout <<" " << *it;}std::cout << '\n';mylist1.unique();for (it = mylist1.begin(); it != mylist1.end(); it++){std::cout << " " << *it;}return 0;
}

但是对于另外一种情况,可能不是很明显,比如如果队列不是有序的,那么去重的效果就会差一点 

所以这个并不能达到无忧无虑的状态,必要情况下需要自己排序

总结

以上就是list的全部内容了,可能还有一些函数没有说明,会放在list模拟实现里面说明,list和之前的vector的操作都是大差不差的,主要的区别在于底层的实现  🎉🎉

相关文章:

【C++】———list容器

前言 1.list容器简单来说其实就是之前的链表结构。 2.这里的list用的是双向带头结点的循环链表。 目录 前言 一 构造函数 1.1 list (); 1.2 list (size_type n, const value_type& val value_type() ); 1.3 list (InputIterator first, InputIterator last…...

【网络安全技术】——期末复习(冲刺篇)

&#x1f4d6; 前言&#xff1a;快考试了&#xff0c;做篇期末总结&#xff0c;都是重点与必考点。 题型&#xff1a;材料分析题、简答题、看图分析题 课本&#xff1a; 目录 &#x1f552; 1. 计算机网络安全概述&#x1f558; 1.1 安全目标&#x1f558; 1.2 常见的网络安全…...

Python中Web开发-Django框架

大家好&#xff0c;本文将带领大家进入 Django 的世界&#xff0c;探索其强大的功能和灵活的开发模式。我们将从基础概念开始&#xff0c;逐步深入&#xff0c;了解 Django 如何帮助开发人员快速构建现代化的 Web 应用&#xff0c;并探讨一些最佳实践和高级技术。无论是初学者还…...

1882java密室逃脱管理系统 Myeclipse开发mysql数据库web结构java编程计算机网页项目

一、源码特点 java密室逃脱管理系统 是一套完善的web设计系统&#xff0c;对理解JSP java编程开发语言有帮助采用了java设计&#xff0c;系统具有完整的源代码和数据库&#xff0c;系统采用web模式&#xff0c;系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开发&…...

LeetCode 两两交换链表中的节点

原题链接24. 两两交换链表中的节点 - 力扣&#xff08;LeetCode&#xff09; 思路&#xff0c;请看图片的过程模拟&#xff0c;这里添加了一个哨兵节点0&#xff0c;目的是为了方便操作&#xff0c;得到指向1节点的指针。 class Solution {public:ListNode* swapPairs(ListNod…...

STM32作业实现(五)温湿度传感器dht11

目录 STM32作业设计 STM32作业实现(一)串口通信 STM32作业实现(二)串口控制led STM32作业实现(三)串口控制有源蜂鸣器 STM32作业实现(四)光敏传感器 STM32作业实现(五)温湿度传感器dht11 STM32作业实现(六)闪存保存数据 STM32作业实现(七)OLED显示数据 STM32作业实现(八)触摸按…...

java mybatis处理大数据量,开启和配置二级缓存,及注意事项,已解决

注意事项&#xff1a; 尽量避免使用下面方式写sql否则会降低服务器性能&#xff1a; mybatis二级缓存开启后&#xff0c;避免使用事务注解&#xff08;加上事务注解后二级缓存数据会导致两次访问不一致问题&#xff09;&#xff1a; 3. 返回的对象实体类&#xff0c;要实现Se…...

在 LLM 架构中应用多专家模型

本文转载自&#xff1a;在 LLM 架构中应用多专家模型 2024年 3月 14日 By Kyle Kranen and Vinh Nguyen https://developer.nvidia.cn/zh-cn/blog/applying-mixture-of-experts-in-llm-architectures/ 文章目录 一、概述二、LLM 架构领域的专家齐聚一堂1、模型容量2、MoE 在降低…...

C语言编程代码软件:深入探索与实战应用

C语言编程代码软件&#xff1a;深入探索与实战应用 在编程的广袤领域中&#xff0c;C语言以其独特的魅力吸引着无数编程爱好者。作为一种基础且强大的编程语言&#xff0c;C语言在软件开发、系统编程、嵌入式系统等领域发挥着不可替代的作用。而要想熟练掌握C语言&#xff0c;…...

【AIGC半月报】AIGC大模型启元:2024.06(上)

AIGC大模型启元&#xff1a;2024.06&#xff08;上&#xff09; (1) ChatTTS&#xff08;语音合成项目&#xff09; (1) ChatTTS&#xff08;语音合成项目&#xff09; 2024.06.01 ChatTTS 文本转语音项目爆火出圈&#xff0c;引来大家极大的关注。短短三天时间&#xff0c;在…...

两款 IntelliJ IDEA 的 AI 编程插件

介绍两款 IntelliJ IDEA 的 AI 编程插件&#xff1a;通义灵码和 CodeGeeX。 通义灵码 这是由阿里推出的一个基于通义大模型的 AI 编码助手。 它提供了代码智能生成、研发智能问答等功能。通义灵码经过海量优秀开源代码数据训练&#xff0c;可以根据当前代码文件及跨文件的上下…...

语义化版本控制:软件工程的实用之道

语义化版本控制&#xff1a;软件工程的实用之道 在软件开发过程中&#xff0c;版本控制是确保项目稳定、有序进行的关键环节。随着项目的发展&#xff0c;功能的增加、错误的修复以及API的修改变得日益频繁。为了有效管理这些变化&#xff0c;并确保团队成员、用户以及依赖该软…...

Java设计模式总结

《武林外传》老白曾经说过这样一句话。高手就是手里无刀&#xff0c;心中也无刀。 类似于设计模式&#xff0c;你不知不觉中已经融进你的代码中了&#xff0c;但你并不知已经运用了。下面我总结几个我觉得比较常用的设计模式。 1&#xff1a;设计模式分类 总体来说设计模式分为…...

小米路由器如何设置去广告功能,如何设置小米路由器的自定义Hosts(小米路由器如何去除小米广告、去除小米电视盒子开屏广告、视频广告)

文章目录 📖 介绍 📖🏡 演示环境 🏡📒 实现方案 📒📝 操作步骤📝 注意事项⚓️ 相关链接 ⚓️📖 介绍 📖 小米设备的广告一直是用户头疼的问题,无论是开屏广告、应用内广告还是系统广告,都影响了用户体验。本文将详细介绍如何通过小米路由器实现去除广告…...

HCIP-Datacom-ARST自选题库__EBGP【18道题】

一、单选题 1.在排除EBGP邻居关系故障时&#xff0c;你发现两台直连设备使用Loopback口建立连接&#xff0c;故执行display current-configurationconfiguration bgp查看peer ebgp-max-hop hop-count的配置&#xff0c;下列哪项说法是正确的? hop-count必须大于2 hop-count…...

TypeScript算法每日一题:两数之和(167)

作者&#xff1a;前端小王hs 阿里云社区博客专家/清华大学出版社签约作者✍/CSDN百万访问博主/B站千粉前端up主 题库&#xff1a;力扣 题目序号&#xff1a;167&#xff08;简单&#xff09; 题目&#xff1a; 给你一个下标从1开始的整数数组 numbers &#xff0c;该数组已按非…...

用docker搭建的Vulfocus镜像管理界面没有镜像可以拉取解决办法

ps&#xff1a;截止到今天2023.4.2&#xff0c;kali和vps的docker拉取的vulfocus镜像会有版本的区别&#xff0c;虽然都是拉取的最新版&#xff0c;vps上镜像为3个月以前&#xff0c;kali上为16个月以前&#xff0c;所以在修改 views.py 文件时&#xff0c;可能会发现文件内容不…...

CSPM.pdf

PDF转图片 归档&#xff1a;...

多个短视频剪辑成一个视频:四川京之华锦信息技术公司

多个短视频剪辑成一个视频&#xff1a;创作中的艺术与技术 在数字时代&#xff0c;短视频以其短小精悍、内容丰富的特点&#xff0c;迅速成为社交媒体上的热门内容形式。然而&#xff0c;有时单一的短视频难以完全表达创作者的意图或满足观众的观赏需求。因此&#xff0c;将多…...

算法第三天力扣第69题:X的平方根

69. x 的平方根 (可点击下面链接或复制网址进行做题) https://leetcode.cn/problems/sqrtx/https://leetcode.cn/problems/sqrtx/ 给你一个非负整数 x &#xff0c;计算并返回 x 的 算术平方根 。 由于返回类型是整数&#xff0c;结果只保留 整数部分 &#xff0c;小数部分将被…...

CTF show Web 红包题第六弹

提示 1.不是SQL注入 2.需要找关键源码 思路 进入页面发现是一个登录框&#xff0c;很难让人不联想到SQL注入&#xff0c;但提示都说了不是SQL注入&#xff0c;所以就不往这方面想了 ​ 先查看一下网页源码&#xff0c;发现一段JavaScript代码&#xff0c;有一个关键类ctfs…...

ubuntu搭建nfs服务centos挂载访问

在Ubuntu上设置NFS服务器 在Ubuntu上&#xff0c;你可以使用apt包管理器来安装NFS服务器。打开终端并运行&#xff1a; sudo apt update sudo apt install nfs-kernel-server创建共享目录 创建一个目录用于共享&#xff0c;例如/shared&#xff1a; sudo mkdir /shared sud…...

React第五十七节 Router中RouterProvider使用详解及注意事项

前言 在 React Router v6.4 中&#xff0c;RouterProvider 是一个核心组件&#xff0c;用于提供基于数据路由&#xff08;data routers&#xff09;的新型路由方案。 它替代了传统的 <BrowserRouter>&#xff0c;支持更强大的数据加载和操作功能&#xff08;如 loader 和…...

12.找到字符串中所有字母异位词

&#x1f9e0; 题目解析 题目描述&#xff1a; 给定两个字符串 s 和 p&#xff0c;找出 s 中所有 p 的字母异位词的起始索引。 返回的答案以数组形式表示。 字母异位词定义&#xff1a; 若两个字符串包含的字符种类和出现次数完全相同&#xff0c;顺序无所谓&#xff0c;则互为…...

实现弹窗随键盘上移居中

实现弹窗随键盘上移的核心思路 在Android中&#xff0c;可以通过监听键盘的显示和隐藏事件&#xff0c;动态调整弹窗的位置。关键点在于获取键盘高度&#xff0c;并计算剩余屏幕空间以重新定位弹窗。 // 在Activity或Fragment中设置键盘监听 val rootView findViewById<V…...

python执行测试用例,allure报乱码且未成功生成报告

allure执行测试用例时显示乱码&#xff1a;‘allure’ &#xfffd;&#xfffd;&#xfffd;&#xfffd;&#xfffd;ڲ&#xfffd;&#xfffd;&#xfffd;&#xfffd;ⲿ&#xfffd;&#xfffd;&#xfffd;Ҳ&#xfffd;&#xfffd;&#xfffd;ǿ&#xfffd;&am…...

html css js网页制作成品——HTML+CSS榴莲商城网页设计(4页)附源码

目录 一、&#x1f468;‍&#x1f393;网站题目 二、✍️网站描述 三、&#x1f4da;网站介绍 四、&#x1f310;网站效果 五、&#x1fa93; 代码实现 &#x1f9f1;HTML 六、&#x1f947; 如何让学习不再盲目 七、&#x1f381;更多干货 一、&#x1f468;‍&#x1f…...

AirSim/Cosys-AirSim 游戏开发(四)外部固定位置监控相机

这个博客介绍了如何通过 settings.json 文件添加一个无人机外的 固定位置监控相机&#xff0c;因为在使用过程中发现 Airsim 对外部监控相机的描述模糊&#xff0c;而 Cosys-Airsim 在官方文档中没有提供外部监控相机设置&#xff0c;最后在源码示例中找到了&#xff0c;所以感…...

Caliper 负载(Workload)详细解析

Caliper 负载(Workload)详细解析 负载(Workload)是 Caliper 性能测试的核心部分,它定义了测试期间要执行的具体合约调用行为和交易模式。下面我将全面深入地讲解负载的各个方面。 一、负载模块基本结构 一个典型的负载模块(如 workload.js)包含以下基本结构: use strict;/…...

Vite中定义@软链接

在webpack中可以直接通过符号表示src路径&#xff0c;但是vite中默认不可以。 如何实现&#xff1a; vite中提供了resolve.alias&#xff1a;通过别名在指向一个具体的路径 在vite.config.js中 import { join } from pathexport default defineConfig({plugins: [vue()],//…...