【C++】list的介绍及使用说明

目录

00.引言

01.list的介绍

模版类

独立节点存储

list的使用

1.构造函数

2.迭代器的使用

分类

运用

3.容量管理

empty():

size():

4.元素访问

5.增删查改


00.引言

我们学习数据结构时,学过两个基本的数据结构:顺序表链表 。顺序表中的数据是连续存储的,并且支持随机访问,但是增删数据的效率比较低,因为要挪动数据;链表中的数据是分散存储的,每个节点包含一个数据和指向令一个节点的指针,不支持随机访问,但插入和删除操作的效率较高。

vector容器内部数据存储原理可以认为是顺序表,而list可以认为是双向链表(每个节点都包含指向前一个和后一个节点的指针)。

01.list的介绍

模版类

和"std::vector"一样,"std::list"实现不同类型数据的管理也是通过模版类 的机制实现的。当创建一个list对象时,可以通过模版参数来指定存储的元素类型。使用**"<数据类型>"**来显式实例化指定存储的元素类型,例如"std::list<int>"表示存储整数类型的链表,"std::list<double>"表示存储双精度浮点型的链表......

cpp 复制代码
#include <iostream>

template<typename T>
class list {
    ......
};

int main() {
    list<int> myList1;
    list<double> myList2;

    return 0;
}

独立节点存储

list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向其前一个元素和后一个元素。因此list可以在任意位置进行插入和删除,且时间复杂度为O(1),并且该容器可以前后双向迭代。

list的使用

在cpluscplus网站上有具体的list的使用说文档:list使用文档,在实际运用中,需要熟悉一些常用的list接口,以下列出了一些常见接口:

1.构造函数

在使用list创建对象之前,需要包含头文件**"<vector>"。**

我们需要调用构造函数来创建对象,list的构造函数有以下几种:

1.list():无参构造

2.list(size_type n,const value_type& val = value_type()):初始化n个val的元素

3.list(const list& x):拷贝构造函数

4.list(inputiterator first, inputiterator last):用(first,last)区间中的元素构造

代码演示:

cpp 复制代码
void text1()
{
    list<int> l1;
    list<int> l2(5, 10);
    list<int> l3(l2);
    list<int> l4(l3.begin(), l3.end());

    int arr[] = { 0,1,2,3,4 };
    list<int> l5(arr, arr + 5);
}

运行结果:

2.迭代器的使用

可以将 list 中的迭代器理解为一个指针,该指针指向list中的某一个节点

分类

**·**begin + end:返回第一个数据位置的迭代器 + 返回最后一个数据的下一个位置的迭代器

**·**rbegin + rend:返回第一个元素的reverse_iterator,即end位置 + 返回最后一个元素下一个位置的reverse_iterator,即begin位置

注意:

1.begin与end为正向迭代器,对迭代器执行++操作,迭代器向后移动

2.rbegin(end)与rend(begin)为反向迭代器,对迭代器执行++操作,迭代器向前移动

运用

遍历list只能用迭代器和范围for,而vector还可以用类似与数组的下标[]访问,那是因为vector中的数据是连续存储的,而list中是分散存储的,元素地址直接并无关联。

迭代器和范围for遍历:

cpp 复制代码
void text2(const list<int>& l)
{
    for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it) {
        cout << *it << " ";
        //*it = 10; 编译不通过
    }
    cout << endl;
}
int main() {
    //text1();
    list<int> l{ 1,2,3,4,5,6,7,8,9 };
    text2(l);
    return 0;
}

运行结果:

可以看到这里使用的是const迭代器,这表示迭代器指向的元素是不可被修改的,来看看const_iterator的底层是这样定义的:

cpp 复制代码
class list {
public:
    // other members...

    // 内部定义 const_iterator
    typedef const T* const_iterator;
};

所以const迭代器指向的元素是常量,不可进行修改操作,而迭代器本身是可以++或--的,如果是

cpp 复制代码
typedef T* const const_iterator;

这种方式定义,则表示迭代器本身不可++或--操作,其指向的元素则可以被修改。

正向反向迭代器:

cpp 复制代码
void text3()
{
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    list<int> l(array, array + sizeof(array) / sizeof(array[0]));
    // 使用正向迭代器正向list中的元素
    // list<int>::iterator it = l.begin();   // C++98中语法
    auto it = l.begin();                     // C++11之后推荐写法
    while (it != l.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    // 使用反向迭代器逆向打印list中的元素
    // list<int>::reverse_iterator rit = l.rbegin();
    auto rit = l.rbegin();
    while (rit != l.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;
}

int main() {
    text3();
    return 0;
}

运行结果:

在C++11中引入了auto关键字,编译器会自动推导auto定义的变量的类型,具体内容可以参考我的这篇博客:auto关键字与基于范围的for循环(C++11)

3.容量管理

list虽然不需要动态开辟管理内存空间,但是还是要对节点的数量(数据的个数)进行跟踪管理

empty():

用于检查列表是否为空。如果列表为空,则返回 'true',否则返回 'false'。

代码演示:

cpp 复制代码
void text4()
{
    list<int> myList;
    if (myList.empty()) {
        std::cout << "List is empty!" << std::endl;
    }
    else {
        std::cout << "List is not empty. Size: " << myList.size() << std::endl;
    }

}

int main() {
    text4();
    return 0;
}

运行结果:

size():

用于获取列表中的数量。

代码演示:

cpp 复制代码
void text5()
{
    list<int> myList = { 1, 2, 3, 4, 5 };
    std::cout << "Size of list: " << myList.size() << std::endl;
}

int main() {
    text5();
    return 0;
}

运行结果:

4.元素访问

有别与begin、end,list中可以通过front、back函数分别获取list的第一个节点的引用 和最后一个节点的引用,是直接获取元素本身,而不是元素地址(指向元素的迭代器(指针))。

代码演示:

cpp 复制代码
void text6()
{
    list<int> myList = { 1, 2, 3, 4, 5 };
    int firstElement = myList.front();
    cout << "First element: " << firstElement << endl;
    int lastElement = myList.back();
    cout << "Last element: " << lastElement << endl;

}

int main() {
    text6();
    return 0;
}

5.增删查改

下面是代码演示:

cpp 复制代码
void PrintList(const list<int>& l)
{
    // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
    for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
    {
        cout << *it << " ";
        // *it = 10; 编译不通过
    }

    cout << endl;
}

// list插入和删除
// push_back/pop_back/push_front/pop_front
void Test1()
{
    int array[] = { 1, 2, 3 };
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));

    // 在list的尾部插入4,头部插入0
    L.push_back(4);
    L.push_front(0);
    PrintList(L);

    // 删除list尾部节点和头部节点
    L.pop_back();
    L.pop_front();
    PrintList(L);
}

// insert /erase 
void Test2()
{
    int array1[] = { 1, 2, 3 };
    list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));

    // 获取链表中第二个节点
    auto pos = ++L.begin();
    cout << *pos << endl;

    // 在pos前插入值为4的元素
    L.insert(pos, 4);
    PrintList(L);

    // 在pos前插入5个值为5的元素
    L.insert(pos, 5, 5);
    PrintList(L);

    // 在pos前插入[v.begin(), v.end)区间中的元素
    vector<int> v{ 7, 8, 9 };
    L.insert(pos, v.begin(), v.end());
    PrintList(L);

    // 删除pos位置上的元素
    L.erase(pos);
    PrintList(L);

    // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
    L.erase(L.begin(), L.end());
    PrintList(L);
}

// resize/swap/clear
void Test3()
{
    // 用数组来构造list
    int array1[] = { 1, 2, 3 };
    list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
    PrintList(l1);

    // 交换l1和l2中的元素
    list<int> l2;
    l1.swap(l2);
    PrintList(l1);
    PrintList(l2);

    // 将l2中的元素清空
    l2.clear();
    cout << l2.size() << endl;
}

int main() {
    Test1();
    Test2();
    Test3();
    return 0;
}

运行结果:

以上就是关于list的介绍和使用说明的有关知识了,欢迎在评论区留言~感觉这篇博客对您有帮助的可以点赞关注支持一波喔~😉

相关推荐
多多*4 分钟前
LUA+Reids实现库存秒杀预扣减 记录流水 以及自己的思考
linux·开发语言·redis·python·bootstrap·lua
Wish3D1 小时前
阿里云OSS 上传文件 Python版本
开发语言·python·阿里云
凤年徐1 小时前
【数据结构初阶】单链表
c语言·开发语言·数据结构·c++·经验分享·笔记·链表
oioihoii1 小时前
C++11 右值引用:从入门到精通
开发语言·c++
朝新_4 小时前
【多线程初阶】阻塞队列 & 生产者消费者模型
java·开发语言·javaee
立莹Sir4 小时前
Calendar类日期设置进位问题
java·开发语言
木子.李3475 小时前
排序算法总结(C++)
c++·算法·排序算法
风逸hhh5 小时前
python打卡day46@浙大疏锦行
开发语言·python
火兮明兮6 小时前
Python训练第四十三天
开发语言·python
freyazzr6 小时前
C++八股 | Day2 | atom/函数指针/指针函数/struct、Class/静态局部变量、局部变量、全局变量/强制类型转换
c++