STL中的list

1、list概述
list的优点是每次插入或删除一个元素,就配置或释放一个元素空间,对于任何位置的元素插入或元素移除,list永远是常数时间。list迭代器是提供使用者使用list的一种接口。

2、list的底层结构
list的底层是一个带头结点的双向环状链表,因此在其任意位置进行数据插入和删除操作都非常方便,时间复杂度均为O(1)。其底层结构如下所示:
在这里插入图片描述

3、list的操作
(1)list的迭代器
1)begin:首端
2)end:结尾
3)rbegin:反向首端
4)rend:反向尾部
注意:rbegin和rend需要使用reverse_iterator迭代器。

(2)成员函数
1)front:返回list中第一个元素的引用
2)back:返回list中最后一个元素的引用
3)push_front:头插入
4)pop_front:头删除
5)push_back:从尾部插入
6)pop_back:从尾部删除
7)insert:任意位置的插入
8)erase:任意位置的删除
9)swap:交换两个list
10)clear:将list中元素清空
11)remove:按条件删除list中的元素
12)remove_if():按()内的条件过滤list中的元素
13)unique:删除重复元素,即将list中连在一起的相同数据删除,只保留第一个。
A:iterator
unique(iterator it_1,iterator it_2):表示对容器中[it_1,it_2)范围的元素进行去重;
B:iterator
unique(iterator it_1,iterator it_2,bool MyFunc):第三个参数指自定义两个元素相等的规则,来对容器中元素进行去重。
14)merge:合并两个有序链表,合并好之后依然有序。
15)sort:单链表排序。
16)reverse:链表的逆置。
17)splice(c1.beg, c2):将c2连接在c1的beg位置,释放c2。
18)empty:检测是否为空。
19)size:返回list中有效元素个数。
20)resize:重置list中有效元素个数。

4、实例验证
【例1】验证list的构造、遍历

#include<stdlib.h>
#include<list>
#include<iostream>
 
using namespace std;

void TestList1()
{
    //构造空的list
    list<int> L1;

    //构造10个值为1的list
    list<int> L2(10, 1);

    //构造用一段区间中的元素构造list
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    list<int> L3(array, array + sizeof(array) / sizeof(array[0]));
 
    //用L3去构造L4
    list<int> L4(L3);

    cout << "L1中元素的个数:" << L1.size() << endl;
    cout << "L2中元素的个数:" << L2.size() << endl;
    cout << "L3中元素的个数:" << L3.size() << endl;
    cout << "L4中元素的个数:" << L4.size() << endl;

    //遍历list1
    list<int>::iterator it = L1.begin();
    cout << "list1 data:";
    while (it != L1.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    //遍历list2
    it = L2.begin();
    cout << "list2 data:";
    while (it != L2.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    //遍历list3
    it = L3.begin();
    cout << "list3 data:";
    while (it != L3.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    //遍历list4
    it = L4.begin();
    cout << "list4 data:";
    while (it != L4.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    //逆向遍历list4
    list<int>::reverse_iterator it5 = L4.rbegin();
    cout << "逆向遍历list4 data:";
    while (it5 != L4.rend())
    {
        cout << *it5 << " ";
        it5++;
    }
    cout << endl;
}

int main()
{
    TestList1();
    system("pause");
    return 0;
}

运行结果为:
在这里插入图片描述
注意:
1)访问rend和rbegin需要使用reverse_iterator迭代器。
2)end的数据不可访问。

【例2】测试链表的插入和删除

void TestList2()
{
    //构造空的list
    list<int> L;

    //尾插
    L.push_back(1);
    L.push_back(2);
    L.push_back(3);
    L.push_back(4);
    L.push_back(5);
    L.push_back(6);

    list<int>::iterator it = L.begin();
    cout << "list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    //尾删
    L.pop_back();
    it = L.begin();
    cout << "尾删后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;

    //头插一个值为0的元素
    L.push_front(0);
    it = L.begin();
    cout << "头插一个值为0的元素后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;

    //头删
    L.pop_front();
    it = L.begin();
    cout << "头删后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;

    //任意位置的插入insert,该函数返回插入的新节点
    list<int>::iterator pos = find(L.begin(), L.end(), 5);
    if (pos != L.end())//end()的数据不可访问
    {
        pos = L.insert(pos, 0);
    }
    it = L.begin();
    cout << "在任意位置insert一个元素后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    //任意位置删除
    L.erase(pos);
    it = L.begin();
    cout << "任意位置删除后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    //给list重新赋值
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    L.assign(array, array + sizeof(array) / sizeof(array[0]));
    it = L.begin();
    cout << "给list重新赋值后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    cout << "获取第一个元素:" << L.front() << endl;
    cout << "获取链表最后一个元素:" << L.back() << endl;

}

运行结果为:
在这里插入图片描述
【例3】测试remove和remove_if

void TestList3()
{
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));
    cout << "list data:";
    list<int>::iterator it = L.begin();
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;
 
    //使用remove删除一个元素
    L.remove(3);
    it = L.begin();
    cout << "使用remove删除一个元素后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;
 
    //使用remove_if删除一个元素
    L.remove_if(Odd());
    cout << "使用remove_if后,list data:";
    it = L.begin();
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

运行结果为:
在这里插入图片描述
【例4】测试unique和sort

void TestList4()
{
    int array[] = { 1, 2, 3, 1, 2, 3, 4, 5, 6, 6, 6, 6 };
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));
    list<int>::iterator it = L.begin();
    cout << "list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;

    //unique
    L.unique();
    it = L.begin();
    cout << "使用unique后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;
 
    //sort
    L.sort();
    it = L.begin();
    cout << "使用sort后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        it++;
    }
    cout << endl;

    //unique
    L.unique();
    it = L.begin();
    cout << "使用unique后,list data:";
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    //如果list中连续两个数之和为3的倍数时,删除第二个数
    L.unique(Three());
    cout << "使用unique的第三个自定义参数后,list data:";
    it = L.begin();
    while (it != L.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

运行结果为:
在这里插入图片描述
【例5】测试merge和reverse

void TestList5()
{
    list<int> L1;
    L1.push_back(1);
    L1.push_back(2);
    L1.push_back(5);
    L1.push_back(7);
    L1.push_back(7);
    L1.push_back(9);
    list<int>::iterator it = L1.begin();
    cout << "list1 data:";
    while (it != L1.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    list<int> L2;
    L2.push_back(3);
    L2.push_back(4);
    L2.push_back(6);
    L2.push_back(7);
    L2.push_back(8);
    L2.push_back(8);
    it = L2.begin();
    cout << "list2 data:";
    while (it != L2.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
 
    //将两个已序链表合并成一个链表,合并之后依然有序
    L1.merge(L2);
    it = L1.begin();
    cout << "合并后的链表:";
    while (it != L1.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;

    //测试reverse,反转链表
    L1.reverse();
    it = L1.begin();
    cout << "反转链表后:";
    while (it != L1.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
}

运行结果为:
在这里插入图片描述
5、list和vector的比较
在STL中list和vector是两个最常见的容器,相比较而言,vector是连续的线性空间,而list不是。STL中的list是一个双向环状链表,迭代器具备前移和后移的能力。list的一个重要的性质:插入和接合操作都不会造成原有的list迭代器的失效,这在vector是不成立的,因为vector的插入操作可能造成空间的重新分配,导致原有的所有迭代器全部失效。list的元素的删除操作也只有“指向被删除元素”的那个迭代器失效,其它迭代器不受任何影响。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值