C++ 20 list容器

本文详细介绍了C++STL中的list容器,包括其链表结构、构造函数、赋值和交换操作、大小调整、插入与删除元素、数据存取方式以及反转和排序功能。list是一个双向循环链表,适合频繁进行插入和删除操作,但不适合随机访问。
摘要由CSDN通过智能技术生成

目录

一、list容器

1.1 简介

1.2 构造函数

1.3 赋值和交换

1.4 大小操作

1.5 插入和删除

1.6 数据存取

1.7 反转和排序


一、list容器

1.1 简介

① 功能:将数据进行链式存储。

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

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

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

⑤ 添加元素,将原指向下一个元素的指针指向新元素即可,新元素指向下一个元素

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

  1. 双向:每一个指针既指向下一个结点的元素,也指向上一个结点的元素。
  2. 循环:最后一个结点的指针会指向第一个结点的元素,第一个结点的指针会指向最后一个结点的元素。

 

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

  1. 它只能通过指针域一个一个前移/后移去找,而不能连续的内存空间,指针加一个数字,就可以找到数据。

⑧ list的优点:

  1. 采用动态存储分配,不会造成内存浪费和溢出。
  2. 链表执行插入和删除操作十分方便,修改指针即可,不需要移动大量数据。

⑨ list的缺点:

  1. 链表灵活,但是空间(指针域)和时间(遍历)额外耗费较大。

10.list有一个重要的性质,插入操作和删除操作都不会造成原有list迭代器的失效,这在vector中是不成立的,vector当插入操作会创建一个更大的数据内容,而vector容器的迭代器却指向原有内存,所以原有的vector容器失效了。

11.STL中list和vector是两个最长被用的容器,各有优缺点。

1.2 构造函数

① 功能描述:创建list容器。

② 函数原型:

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

③ list构造方式同其他几个STL容器一样。

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

void printList(const list<int>&L)
{
    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(L2);

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

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

int main() {

    test01();

    system("pause");

    return 0;

}
运行结果:

10 20 30 40
10 20 30 40
10 20 30 40
1000 1000 1000 1000 1000 1000 1000 1000 1000 1000
请按任意键继续. . .

1.3 赋值和交换

① 功能描述:给list容器进行赋值,以及交换list容器。

② 函数原型:

  1. assign(beg,end); //将[beg,end)区间中的数据拷贝赋值给本身。
  2. assign(n,elem); //将n个elem拷贝赋值给本身。
  3. list& operator=(const list &list); //重载等号操作符。
  4. swap(list); //将lst与本身的元素互换。
#include<iostream>
using namespace std;
#include <list>

//list容器赋值和交换

void printList(const list<int>&L)
{
    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()
{
    //创建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;

}
运行结果:

10 20 30 40
10 20 30 40
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换前:
10 20 30 40
100 100 100 100 100 100 100 100 100 100
交换后:
100 100 100 100 100 100 100 100 100 100
10 20 30 40
请按任意键继续. . .

1.4 大小操作

① 功能描述:对list容器的大小进行操作。

② 函数原型:

//返回容器中元素的个数。

  1. size();

//判断容器是否为空。

  1. empty();

//重新指定容器的长度为num,若容器变长,则以默认值填充新位置。

//如果容器变短,则末尾超出容器长度的元素被删除。

  1. resize(num);

//重新指定容器的长度为num,若容器变成,则以elem值填充新位置。

//如果容器变短,则末尾超出容器长度的元素被删除。

  1. resize(num,elem);
#include<iostream>
using namespace std;
#include <list>

//list容器赋值和交换

void printList(const list<int>&L)
{
    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,10000);
    printList(L1);

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

int main() {

    test01();
    
    system("pause");

    return 0;

}
运行结果:

10 20 30 40
L1不为空:
L1的元素个数为:4
10 20 30 40 10000 10000 10000 10000 10000 10000
10 20
请按任意键继续. . .

1.5 插入和删除

① 功能描述:对list容器进行数据的插入和删除。

② 函数原型:

  1. push_back(elem); //在容器尾部加入一个元素。
  2. pop_back(); //删除容器中最后一个元素。
  3. push_front(elem); //在容器开头插入一个元素。
  4. pop_front(); //从哪个容器开头移除第一个元素
  5. insert(pos,elem); //在pos位置插elem元素的拷贝,返回新数据的位置。
  6. insert(pos,n,elem); //在pos位置插入n个elem数据,无返回值。
  7. insert(pos,beg,end); //在pos位置插入[beg,end)区间的数据,无返回值。
  8. clear(); //移除容器的所有数据。
  9. erase(beg,end); //删除[beg,end)区间的数据,返回下一个数据的位置。
  10. erase(pos); //删除pos位置的数据,返回下一个数据的位置。
  11. remove(elem); //删除容器中所有与elem值匹配的元素。
#include<iostream>
using namespace std;
#include <list>

//list容器赋值和交换

void printList(const list<int>&L)
{
    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_front(100);
    L1.push_front(200);
    L1.push_front(300);

    //遍历容器
    printList(L1);

    //尾删
    L1.pop_back();
    printList(L1);

    //头删
    L1.pop_front();
    printList(L1);

    //insert插入
    list<int>::iterator it = L1.begin();
    L1.insert(++it, 1000);
    printList(L1);

    //删除
    it = L1.begin();
    L1.erase(it);
    printList(L1);

    //移除
    L1.push_back(10000);
    L1.push_back(10000);
    L1.push_back(10000);
    L1.push_back(10000);
    printList(L1);

    L1.remove(10000);
    printList(L1);

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

}

int main() {

    test01();

    system("pause");

    return 0;

}
运行结果:

300 200 100 10 20 30
300 200 100 10 20
200 100 10 20
200 1000 100 10 20
1000 100 10 20
1000 100 10 20 10000 10000 10000 10000
1000 100 10 20
请按任意键继续. . .

1.6 数据存取

① 功能描述:对list容器中数据进行存取。

② 函数原型:

  1. front(); //返回第一个元素。
  2. back(); //返回最后一个元素。

③ list容器不是连续的内存空间,所以不能通过[]、at等方式随机访问。

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

//list容器  数据存取

void printList(const list<int>&L)
{
    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);

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

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

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

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

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

    //验证迭代器是不支持随机访问的

    list<int>::iterator it = L1.begin();

    it++;  //因为list是双向的,所以支持递增、递减++、--的操作,但是不支持it = it+1;it = it+2;....,即不支持这样的随机访问      

}

int main() {

    test01();

    system("pause");

    return 0;

}
运行结果:

第一个元素为:10
第一个元素为:40
请按任意键继续. . .

1.7 反转和排序

① 功能描述:将容器中的元素反转,以及将容器中的数据进行排序。

② 函数原型:

  1. reverse(); //反转链表
  2. sort(); //链表排序
#include<iostream>
using namespace std;
#include <list>
#include<algorithm>

//list容器  反转和排序

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

bool myCopare(int v1, int v2)
{
    //降序 就让第一个数 大于第二个数为真
    return v1 > v2;

}

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);

    //排序
    cout << "排序前:" << endl;
    printList(L1);

    //所有不支持随机访问迭代器的容器,不可以用标准算法
    //不支持随机访问迭代器的容器,内部会提供对应的一些算法
    //sort(L1.begin(),L1.end());  //报错,这是标准算法,全局函数的sort()
    L1.sort();  //默认排序规则 从小到大 升序
    cout << "排序后:" << endl;
    printList(L1);

    L1.sort(myCopare);  //降序排列 这是成员函数的sort()
    cout << "重载排序算法,降序排序后:" << endl;
    printList(L1);
    
}


int main() 
{
    test01();

    system("pause");

    return 0;
}
运行结果:

反转前:
20 10 50 40 30
反转后:
30 40 50 10 20
排序前:
30 40 50 10 20
排序后:
10 20 30 40 50
重载排序算法,降序排序后:
50 40 30 20 10
请按任意键继续. . .

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值