【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的值初始化third
    std::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);      // 初始化为3
	list<int> second(5);     // 初始化为5

	second = first;//赋值
	first = list<int>();//把一个匿名对象赋值给first

	cout << "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 5

    it = mylist.begin();
    ++it;       
    mylist.insert(it, 10);                        // 1 10 2 3 4 5

                        
    mylist.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 90
    it1 = 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 4

    for (int i = 1; i <= 3; ++i)
        mylist2.push_back(i * 10);   // mylist2: 10 20 30

    it = mylist1.begin();
    ++it;                         // points to 2

    mylist1.splice(it, mylist2); // mylist1: 1 10 20 30 2 3 4
    // mylist2 (empty)
    // it 依然指向2

    mylist2.splice(mylist2.begin(), mylist1, it);
    // mylist1: 1 10 20 30 3 4
    // mylist2: 2
    // it现在已经失效了
    it = mylist1.begin();//重新赋值
    std::advance(it, 3);           // it 指向30

    mylist1.splice(mylist1.begin(), mylist1, it, mylist1.end());
    // mylist1: 30 3 4 1 10 20

    std::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的操作都是大差不差的,主要的区别在于底层的实现  🎉🎉

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/670223.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

21.Redis之分布式锁

1.什么是分布式锁 在⼀个分布式的系统中, 也会涉及到多个节点访问同⼀个公共资源的情况. 此时就需要通过 锁 来做互斥控制, 避免出现类似于 "线程安全" 的问题. ⽽ java 的 synchronized 或者 C 的 std::mutex, 这样的锁都是只能在当前进程中⽣效, 在分布式的这种多…

计算机系统结构之互联网络

一、基本的单级互联网络 1、立方体单级网络 立方体单级网络的名称来源于下图所示的三维立方体结构。每个顶点&#xff08;网络的节点&#xff09;代表一个处理单元&#xff0c;共有8个处理单元&#xff0c;用zyx三位二进制编号。 Cubei函数表式相连的入端和出端的二进制编号只…

海外媒体通稿:9个极具创意的旅游业媒体推广案例分享-华媒舍

如今&#xff0c;旅游业正迅速发展&#xff0c;媒体推广成为吸引游客的关键。为了更好地展示旅游目的地&#xff0c;许多创意而富有创新的媒体推广策略应运而生。本文将介绍九个极富创意的旅游业媒体推广案例&#xff0c;为广大从业者带来灵感和借鉴。 1. 视频系列&#xff1a;…

Hadoop3:MapReduce的序列化和反序列化

一、概念 1、序列化 就是把内存中的对象&#xff0c;转换成字节序列 &#xff08;或其他数据传输协议&#xff09;以便于存储到磁 盘&#xff08;持久化&#xff09;和网络传输。 2、反序列化 就是将收到字节序列&#xff08;或其他数据传输协议&#xff09;或者是磁盘的持…

services层和controller层

services层 我的理解&#xff0c;services层是编写逻辑代码语句最多的一个层&#xff0c;非常重要&#xff0c;在实际的项目中&#xff0c;负责调用Dao层中的mybatis&#xff0c;在我的项目中它调用的是这两个文件 举例代码如下 package com.example.sfdeliverysystem.servic…

华东师范大学研究团队《Ecology Letters 》揭示植物如何改变其物候以响应全球变化

自工业革命以来&#xff0c;人类活动导致多种环境因子同时发生变化&#xff0c;包括气候变暖、降水模式改变、氮沉降增加和大气CO2升高。这些变化预计会影响植物生命周期事件的季节时序—植物物候&#xff08;Nature Reviews Earth & Environment | 傅伯杰院士团队发文阐述…

基于java的CRM客户关系管理系统(二)

目录 第二章 相关技术介绍 2.1 后台介绍 2.1.1 B/S平台模式 2.1.2 MVC 2.1.3 Spring 2.1.4 Hibernate 2.1.5 Struts 2.2 前端介绍 2.2.1 JSP网页技术 2.3 开发工具 2.4 本章小结 前面内容请移步 基于java的CRM客户关系管理系统&#xff08;二&#xff09; 资源…

机器学习第四十一周周报 JTFT

文章目录 week41 JTFT摘要Abstract1. 题目2. Abstract3. 网络架构3.1 JTFT3.2 具有可学习频率的稀疏FD表示3.3 用于提取跨渠道依赖关系的低阶注意力层 4. 文献解读4.1 Introduction4.2 创新点4.3 实验过程 5. 结论小结参考文献 week41 JTFT 摘要 本周阅读了题为A Joint Time-…

【TIPs】 Visual Stadio 2019 中本地误使用“git的重置 - 删除更改 -- hard”后,如何恢复?

环境&#xff1a; VS 2019Windows10本地版本管理&#xff08;非远程&#xff09; 前言&#xff1a; git 在Visual Stadio 2019中集成了git的版本管理&#xff0c;在本地用来做版本管理&#xff0c;本来比较好用。 不过有一次&#xff0c;由于拿最初始的版本的时候&#xf…

fyne apptab布局

fyne apptab布局 AppTabs 容器允许用户在不同的内容面板之间切换。标签要么只是文本&#xff0c;要么是文本和一个图标。建议不要混合一些有图标的标签和一些没有图标的标签。 package mainimport ("fyne.io/fyne/v2/app""fyne.io/fyne/v2/container"//&…

广告变现是什么

广告变现是指媒体或平台通过向用户展示广告主的广告&#xff0c;从而获得收入的过程。 广告变现就像是一个店主&#xff0c;他需要有一个吸引人的店面&#xff0c;提供优质的内容和服务&#xff0c;然后在店里摆放一些别人的商品或服务&#xff0c;每当有客人看了或买了这…

Proxmox 虚拟环境下1Panel Linux 服务器运维管理面板的安装

简介 以前安装服务器管理面板用的都是宝塔&#xff0c;今天发现 1Panel Linux 服务器运维管理面板也很好&#xff0c;面板清晰整洁&#xff0c;使用的技术比较先进&#xff0c;所以我决定亲自安装一下看看效果就竟如何&#xff1f; 1Panel Linux 服务器运维管理面板是一个开源…

C语言 | Leetcode C语言题解之第125题验证回文串

题目&#xff1a; 题解&#xff1a; bool isalumn(char c) {return (c > a && c < z) || (c > A && c < Z) || (c > 0 && c < 9); }bool isPalindrome(char* s) {for (int left 0, right strlen(s) - 1; left < right; left, …

XDMA原理及其应用和发展

XDMA原理 XDMA的主要原理是通过直接访问主机内存&#xff0c;实现数据的快速传输。在传统的DMA&#xff08;Direct Memory Access&#xff09;技术中&#xff0c;数据传输需要经过CPU的干预&#xff0c;而XDMA可以绕过CPU&#xff0c;直接将数据从外设读取到主机内存或者从主机…

Java | Leetcode Java题解之第126题单词接龙II

题目&#xff1a; 题解&#xff1a; class Solution {public List<List<String>> findLadders(String beginWord, String endWord, List<String> wordList) {List<List<String>> res new ArrayList<>();// 因为需要快速判断扩展出的单词…

7-zip安装教程

一、简介 7-Zip 是一款开源的文件压缩软件&#xff0c;由 Igor Pavlov 开发。它具有高压缩比、支持多种格式、跨平台等特点。使用 C语言编写&#xff0c;其代码在 Github 上开源。 7-Zip的官网&#xff1a; 7-Zip 7-zip官方中文网站&#xff1a; 7-Zip 官方中文网站 7-Zip 的 G…

Java | Leetcode Java题解之第125题验证回文串

题目&#xff1a; 题解&#xff1a; class Solution {public boolean isPalindrome(String s) {int n s.length();int left 0, right n - 1;while (left < right) {while (left < right && !Character.isLetterOrDigit(s.charAt(left))) {left;}while (left …

Java筑基-集合[Set、Map、List、Stack、Queue]

这里写目录标题 一、Collection接口结构图二、Set集合1、常用方法 三、List集合1、List集合常用方法2、代码案例 四、Stack集合1、方法2、代码展示 五、Queue集合1、常用的方法2、代码展示 六、Map集合1、基本概念2、常用方法3、代码展示 一、Collection接口结构图 二、Set集合…

C语言中 printf函数格式化输出

一. 简介 本文来简单学习一下&#xff0c;C语言中printf函数格式化输出时&#xff0c;因为我们的粗心没有 将数据类型与格式化参数对应&#xff0c;而导致的一些问题。 二. C语言中printf函数的格式化输出 在C语言中&#xff0c;printf函数是用于格式化输出的函数&#xff0…

如何实现一个AI聊天功能

最近公司的网站上需要对接一个AI聊天功能&#xff0c;领导把这个任务分给了我&#xff0c;从最初的调研&#xff0c;学习&#xff0c;中间也踩过一些坑&#xff0c;碰到过问题&#xff0c;但最后对接成功&#xff0c;还是挺有成就感的&#xff0c;今天把这个历程和项目整理一下…