List容器

本文详细介绍了C++ STL中的List容器,包括其基本概念、构造函数、赋值与交换、容量和大小、插入和删除、数据存取、反转与排序等操作,并提供了多个实例代码进行说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、list容器的基本概念。
1、功能: 将数据进行链式储存。

2、组成。
链表的组成: 由一系列的节点组成。
节点的组成: 一个储存数据元素的数据域,和一个储存下一个节点的地址的指针域。
STL中的链表是一个双向循环链表。

3、迭代器。
由于链表不是一段连续的储存空间,因此链表的迭代器只支持前移和后移,属于双向迭代器。

二、构造函数、赋值、交换、容量和大小。
#include <iostream>
using namespace std;
#include <list>

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

2、赋值与交换。
assign(beg,end);   //将[beg,end)区间中的数据拷贝赋值给本身。
assign(n,elem);    //将n个elem拷贝赋值给本身
list& operator=(const list &lst); //重载等号操作符
swap(lst);  //将lst与本身的元素互换。

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

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

int main()
{
    //1. 创建list容器
    list<int> L1;  //默认构造

    //2. 添加数据
    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(10,1000);
    printList(L3);

    list<int> L4(L3);
    printList(L4);

    list<int> L5;
    L5.assign(L4.begin(),L4.end());
    printList(L5);

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

    list<int> L7;
    L7 = L6;
    printList(L7);

    L1.swap(L7);
    printList(L1);
    printList(L7);

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

    //重新指定大小
    L1.resize(15,2000);
    printList(L1);

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

    return 0;
}

三、插入和删除、数据存取
#include <iostream>
using namespace std;
#include <list>

/*
4、插入和删除。
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(pos); //删除pos位置的数据,返回下一个数据的位置
remove(elem); /删除容器中所有与elem匹配的元素。

5、数据存取。
front();   //返回第一个元素
back();    //返回最后一个元素
*/

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

int main()
{
    list<int> L;

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

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

    printList(L);

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

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

    //insert
    L.insert(L.begin(),1000);

    L.insert(L.begin(),2,200);
    printList(L);

    //删除
    list<int>::iterator it = L.begin();
    L.erase(++it);
    printList(L);

    L.remove(200);
    printList(L);
    //L.clear();
    //printList(L);

    cout << "第一个元素为:" << L.front() << endl;
    cout << "最后一个元素为:" << L.back() << endl;

    return 0;
}

四、反转和排序。
#include <iostream>
using namespace std;
#include <list>
#include <algorithm>

/*
6、反转与排序。
reverse();  //反转链表
sort();     //链表排序
*/

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

    //所有不支持随机访问迭代器的容器,不可以用标准算法sort()
    //sort(L1.begin(),L1.end());

    //不支持随机访问的迭代器的容器,内部会提供对应的一些算法。
    //L1.sort();  //默认是升序的
    L1.sort(mycompare);

    cout << "排序后:" << endl;
    printList(L1);
}

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

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值