C++中list容器语法及接口使用方法(附C++代码)

在这里插入图片描述

💪 图像算法工程师,专业从事且热爱图像处理,图像处理专栏更新如下👇:
📝《图像去噪》
📝《超分辨率重建》
📝《语义分割》
📝《风格迁移》
📝《目标检测》
📝《图像增强》
📝《模型优化》
📝《模型实战部署》
📝《图像配准融合》
📝《数据集》
📝《高效助手》
📝《C++》


在这里插入图片描述

std::list 是 C++ 标准模板库(STL)中的一种双向链表容器。它提供动态大小调整的序列,支持快速的插入和删除操作,尤其适用于需要频繁在中间或两端插入/删除的场景。

一、list容器概念

1.1 概念

将数据进行链式存储。

链表(list)是一种物理存储单元上非连续的存储结构,数据元素的逻辑顺序是通过链表中的指针链接实现的。

链表的组成:链表由一系列结点组成。

结点的组成:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域

STL中的链表是一个双向循环链表 。

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。

1.2 特点

list容器的特点见下:

双向链表: 每个节点包含指向前一个节点和后一个节点的指针,便于双向遍历。

动态内存分配: 容器的大小可以动态增长,无需预分配内存,不会造成内存浪费和溢出。

非连续存储: 元素在内存中不是连续存储,访问效率低于 vector。空间(指针域)和时间(遍历)额外耗费较大。

高效插入/删除: 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量元素。在任意位置插入或删除元素的时间复杂度为 O ( 1 ) O(1) O(1)

:list有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector是不成立的。

性能优势: 适用于需要频繁在中间插入或删除的场景。

顺序操作: 不支持随机访问,无法使用 [] 或 at() 直接访问元素。

效率对比: 对于频繁查询和访问的需求,vector 通常比 list 更高效。

二、list容器构造函数

list容器构造函数原型:

list<T> lst;                  // list采用采用模板类实现,对象的默认构造形式:
list(beg,end);            //构造函数将[beg, end)区间中的元素拷贝给本身
list(n,elem);               //构造函数将n个elem拷贝给本身。
list(const list &lst);     //拷贝构造函数。

2.1 示例代码

list容器构造函数示例代码见下:

#include <iostream>
using namespace std;
#include <list>

// list容器构造函数

// void printList(list<int>&L)
// {
//     for (list<int>::iterator it = L.begin();it != L.end();it++)
//     {
//         cout << *it << "    ";
//     }
//     cout << endl;
// }

void printList(const list<int>&L)         // 加了const表示只读模式
{
    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(L1);

    // 拷贝构造
    list<int>L3(L2);
    printList(L3);

    // n个elem
    list<int>L4(10,1000);    // 10个1000
    printList(L4);

}

int main()
{
    test01();

    system("pause");
    return 0;
}

2.2 输出

运行上面2.1中代码,输出见下:

在这里插入图片描述

三、list容器赋值和交换

给list容器进行赋值,以及交换list容器。

函数原型:

assign(beg,end);						//将[beg,end)区间中的数据拷贝赋值给本身
assign(n, elem);                      //将n个elem拷贝赋值给本身
list& operator=(const list &lst);     //重载等号操作符

swap(lst);					//将lst与本身的元素互换

3.1 示例代码

下面是list容器赋值和交换的示例代码:

#include <iostream>
using namespace std;
#include <list>

// list容器赋值和交换操作

void printList(const list<int>&L)         // 加了const表示只读模式
{
    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;
    L2 = L1;    // operator =   赋值
    printList(L2);

    list<int> L3;
    L3.assign(L2.begin(),L2.end());
    printList(L3);

    list<int> L4;
    L4.assign(10,100);
    printList(L4);

}

void test02()
{
    cout << "下面输出是交换函数中的内容" << endl;
    // 创建list容器
    list<int> L1;  // 默认构造函数

    // 添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);

    list<int> L2;
    L2.assign(10,100);

    cout << "交换前:" << endl;
    printList(L1);
    printList(L2);

    L1.swap(L2);     //交换
    cout << "交换后:" << endl;
    printList(L1);
    printList(L2);
}

int main()
{
    test01();
    test02();

    system("pause");
    return 0;
}

3.2 输出

运行3.1中代码,输出见下:

在这里插入图片描述

四、list容器大小操作和判断是否为空

对list容器的大小进行操作,并判断list容器是否为空。

函数原型:

size();							//返回容器中元素的个数
empty(); 						//判断容器是否为空
resize(num);				//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。
resize(num, elem);		//重新指定容器的长度为num,若容器变长,则以elem值填充新位置。如果容器变短,则末尾超出容器长度的元素被删除。

4.1 示例代码

list容器大小操作和判断是否为空的示例代码见下:

#include <iostream>
using namespace std;
#include <list>

// list容器大小和空判断

void printList(const list<int>&L)         // 加了const表示只读模式
{
    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);

   // 判断容器是否为空
   if (L1.empty())
   {
        cout << "L1为空!" << endl; 
   }
   else
   {
        cout << "L1不为空!" << endl;
        cout << "L1的元素个数为:" << L1.size() << endl;
   }

   // 重新指定大小
   L1.resize(10,999);         // 指定list容器大小为10,没有元素的位置用999填充
   printList(L1);

   L1.resize(2);
   printList(L1);

}


int main()
{
    test01();

    system("pause");
    return 0;
}

4.2 输出

运行上面4.1中代码,输出见下:

在这里插入图片描述

五、list容器插入和删除

对list容器进行数据的插入和删除。

函数原型:

push_back(elem);					//在容器尾部加入一个元素
pop_back();								//删除容器中最后一个元素
push_front(elem);					//在容器开头插入一个元素
pop_front();								//从容器开头移除第一个元素
insert(pos,elem);						//在pos位置插elem元素的拷贝,返回新数据的位置
insert(pos,n,elem);					//在pos位置插入n个elem数据,无返回值
insert(pos,beg,end);				//在pos位置插入[beg,end)区间的数据,无返回值
clear();										//移除容器的所有数据
erase(beg,end);						//删除[beg,end)区间的数据,返回下一个数据的位置
erase(pos);								//删除pos位置的数据,返回下一个数据的位置
remove(elem);							//删除容器中所有与elem值匹配的元素

5.1 示例代码

list容器中插入和删除数据的示例代码见下:

#include <iostream>
using namespace std;
#include <list>

// list容器插入和删除

void printList(const list<int>&L)         // 加了const表示只读模式
{
    for (list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout << *it << "    ";
    }
    cout << endl;
}

void test01()
{
    // 创建list容器
    list<int> L;  // 默认构造函数

    // 尾插
    L.push_back(10);
    L.push_back(20);
    L.push_back(30);

    // 头插
    L.push_front(100);
    L.push_front(200);
    L.push_front(300);

    // 遍历容器
    printList(L);   // 300 200 100 10 20 30

    // 尾删
    L.pop_back();
    printList(L);    // 300 200 100 10 20

    // 头删
    L.pop_front();
    printList(L);     // 200 100 10 20

    // 插入
    L.insert(L.begin(),1000);   
    printList(L);     // 1000 200 100 10 20

    list<int>::iterator it = L.begin();  // 利用迭代器移位插值
    L.insert(++it,2000);
    printList(L);     // // 1000 2000 200 100 10 20

    // 删除
    it = L.begin();
    // L.erase(it);
    // printList(L);        // 2000 200 100 10 20

    L.erase(++it);   // 移位删除
    printList(L);        // 1000  200 100 10 20

    // 移除
    L.push_back(10000);     // 向list容器中添加元素10000
    L.push_back(10000);     // 向list容器中添加元素10000
    L.push_back(10000);     // 向list容器中添加元素10000
    L.push_back(10000);     // 向list容器中添加元素10000
    printList(L);           // 1000  200 100 10 20 10000 10000 10000 10000

    L.remove(10000);        // 会将list容器中所有的10000元素移除
    printList(L);           // 1000  200 100 10 20

    // 清空
    L.clear();
    printList(L);

}


int main()
{
    test01();

    system("pause");
    return 0;
}

5.2 输出

运行5.1中代码,输出见下:

在这里插入图片描述

六、list容器数据存取

对list容器中数据进行存取。

函数原型:

front();                           //返回第一个元素
back();                          //返回最后一个元素

6.1 示例代码

list容器数据存取示例代码见下:

#include <iostream>
using namespace std;
#include <list>

// list容器数据存取

void printList(const list<int>&L)         // 加了const表示只读模式
{
    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);

    // L1[0] 不可以用[]访问list容器中的元素

    // L1.at(0) 不可以用at方式访问list容器中的元素

    // 原因是list本质链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的

    cout << "第一个元素为:" << L1.front() << endl;
    cout << "第二个元素为:" << L1.back() << endl;

    // 验证迭代器不支持随机访问的
    list<int>::iterator it = L1.begin();
    it++; // 支持双向
    it--;
    // it = it + 1;   // 不支持随机访问

}


int main()
{
    test01();

    system("pause");
    return 0;
}

6.2 输出

运行6.1中代码,输出见下:

在这里插入图片描述

七、list容器反转和排序

将容器中的元素反转,以及将容器中的数据进行排序。

函数原型:

reverse();           //反转链表
sort();				  //链表排序

补:所有不支持随机访问迭代器的容器,不可以用标准算法。
不支持随机访问迭代器的容器,内部会提供 对应一些算法。

7.1 示例代码

list容器反转和排序的示例代码见下:

#include <iostream>
using namespace std;
#include <list>
#include <algorithm>

// list容器数据存取

void printList(const list<int>&L)         // 加了const表示只读模式
{
    for (list<int>::const_iterator it = L.begin();it != L.end();it++)
    {
        cout << *it << "    ";
    }
    cout << endl;
}

void test01()
{
    // 反转
    list<int> L1;  

    // 添加数据
    L1.push_back(20);
    L1.push_back(10);
    L1.push_back(50);
    L1.push_back(40);
    L1.push_back(30);

    cout << "反转前:" << endl;
    printList(L1);

    // 反转
    L1.reverse();
    cout << "反转后:" << endl;
    printList(L1);

}

bool myCompare(int v1,int v2)
{
    // 降序 让第一个数 > 第二个数
    return v1 > v2;
}

// 排序
void test02()
{
    // 排序
    list<int> L2;  

    // 添加数据
    L2.push_back(20);
    L2.push_back(10);
    L2.push_back(50);
    L2.push_back(40);
    L2.push_back(30);

    cout << "排序:" << endl;
    printList(L2);

    // 所有不支持随机访问迭代器的容器,不可以用标准算法
    // 不支持随机访问迭代器的容器,内部会提供 对应一些算法
    // sort(L2.begin(),L2.end());   // 不对

    L2.sort();   // 默认排序规则 从小到大 升序
    cout << "排序后:" << endl;
    printList(L2);

    // 降序
    cout << "降序结果:" << endl;
    L2.sort(myCompare);
    printList(L2);
}


int main()
{
    test01();
    test02();

    system("pause");
    return 0;
}

7.2 输出

运行上面7.1中代码,输出见下:

在这里插入图片描述

八、总结

以上就是C++中list容器语法及接口使用方法,希望能帮到你!本人参考学习的是黑马程序员,仅作为笔记记录。

感谢您阅读到最后!😊总结不易,多多支持呀🌹 点赞👍收藏⭐评论✍️,您的三连是我持续更新的动力💖

关注下面「视觉研坊」,获取干货教程、实战案例、技术解答、行业资讯!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

视觉研坊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值