C++ list 使用方法

本文详细介绍了C++中list容器的使用方法,包括赋值、遍历、增删改查、排序、合并等操作,并提供了实例代码进行演示。

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

C++ list 使用方法

使用 list需引入头文件#include <list>

引入命名空间 using namespace std;

list函数如下

//assign()      给list赋值
//back()        返回最后一个元素的引用
//begin()       返回指向容器第一个元素的指针
//clear()       删除所有元素
//empty()       如果list是空的则返回true
//end()         返回容器末尾的指针
//erase()       删除一个元素
//front()       返回第一个元素
//get_allocator() 返回list的配置器
//insert()      插入一个元素到容器中
//max_size()    返回list能容纳的最大元素数量
//merge()       合并两个list
//pop_back()    删除最后一个元素
//pop_front()   删除第一个元素
//push_back()   在list的末尾添加一个元素
//push_front()  在list的头部添加一个元素
//rbegin()      返回指向第一个元素的逆向迭代器
//remove()      从list删除元素
//remove_if()   按指定条件删除元素
//rend()        指向list末尾的逆向迭代器
//resize()      改变list的大小
//reverse()     把list的元素倒转
//size()        返回list中的元素个数
//sort()        给list排序
//splice()      合并两个list
//swap()        交换两个list
//unique()      删除list中重复的元素

使用实例如下

// Pro1.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"

#include <list>

using namespace std;

// 创建一个 list 容器的实例 intList
typedef list<int> intList;

bool Is2(int i);

bool Sort(int i, int j);

int main()
{
    intList _intList;

    for (int i = 0; i < 3; ++i)
    {
        // 在容器头部添加一个元素
        _intList.push_front(i);
    }

    for (int i = 0; i < 3; ++i)
    {
        // 在容器末尾添加一个元素
        _intList.push_back(i * 10);
    }

    // 获取容器存储元素的个数
    int size = _intList.size();
    std::cout << "size : " << size <<endl;  // size : 6

    // 获取容器可以存储的最大元素个数 
    int max_size = _intList.max_size();
    std::cout << "max_size : " << max_size << endl;   // max_size : 357913941

    // 声明 it 为容器迭代器
    intList::iterator it;

    // 获取指向容器第一个元素的指针
    //_intList.begin()

    // 获取指向容器最后一个元素 +1 的指针
    // _intList.end()

    // 使用迭代器遍历容器
    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;  // 2, 1, 0, 0, 10, 20
    }

    // front()获取容器第一个元素的引用
    int n = _intList.front();
    std::cout << "n  : "<< n << endl;  // n  : 2

    // front() 作为第一个元素的引用,可以作为左值使用 
    _intList.front() = 111;
    n = _intList.front();
    std::cout << "n  : " << n << endl;  // n  : 111

    // back() 返回最后一个元素的引用
    n = _intList.back();
    std::cout << "n  : " << n << endl;  // n  : 20

    // back() 作为最后一个元素的引用,可以作为左值使用
    _intList.back() = 222;
    n = _intList.back();
    std::cout << "n  : " << n << endl;  // n  : 222

    // 使用迭代器遍历容器
    it = _intList.begin();
    for (; it != _intList.end();)
    {
        // 删除一个元素
        _intList.erase(it++);
    }

    size = _intList.size();
    std::cout << "size : " << size << endl;  // size : 0

    // 在容器第一个元素位置插入数据 1,返回迭代器 it 位置处的指针
    it = _intList.begin();
    it = _intList.insert(it, 1);
    ++it;
    // 在迭代器 it 位置插入数据 2
    it = _intList.insert(it, 2);

    // 在迭代器 it 位置插入 2 个数据 3
    it = _intList.insert(it, 2, 3);

    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 1, 3, 3, 2
    }

    std::cout << "" << endl;   
    // 
    it = _intList.begin();
    // 在_intList.begin()位置开始,插入++it和_intList.end() 之间的数据
    _intList.insert(_intList.begin(), ++it, _intList.end());

    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 3, 3, 2, 1, 3, 3, 2
    }

    // 删除容器第一个元素
    _intList.pop_front();
    // 删除容器最后一个元素
    _intList.pop_back();

    // 删除容器所有元素
    _intList.clear();
    size = _intList.size();
    std::cout << "size : " << size << endl;  // size : 0

    // 判断容器是否为空
    if (_intList.empty())
    {
        std::cout << "_intList is empty" << endl;
    }

    for (int i = 0; i < 3; i ++)
    {
        _intList.insert(it, i);
        _intList.insert(it, i);
    }

    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 0, 0, 1, 1, 2, 2,
    }

    std::cout << "" << endl;   // 0, 0, 1, 1, 2, 2,

    // 删除容器中所有值为 1 的元素
    _intList.remove(1);
    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 0, 0, 2, 2,
    }

    std::cout << "" << endl;

    // 删除容器中符合 函数 Is2 返回 true 的值
    _intList.remove_if(Is2);
    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 0, 0,
    }

    std::cout << "" << endl;

    // 改变容器存储空间大小(可以存储元素个数)
    _intList.resize(10);
    // 改变容器存储空间大小,新添加数据默认填充 100
    _intList.resize(10, 100);

    _intList.clear();

    for (int i = 0; i < 5; i++)
    {
        _intList.push_back(i);
    }

    // 将容器中的数据反转
    _intList.reverse();

    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 4, 3, 2, 1, 0
    }

    std::cout << "" << endl;

    _intList.push_back(0);

    // 删除容器中重复的元素
    _intList.unique();

    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 4, 3, 2, 1, 0
    }

    // 将容器中数据按照 Sort 函数排序
    _intList.sort(Sort);
    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 4, 3, 2, 1, 0
    }
    std::cout << "" << endl;

    intList _intList2;

    _intList2.push_back(100);
    _intList2.push_back(200);
    _intList2.push_back(300);

    // 将容器 _intList2容器合并到 _intList
    _intList.merge(_intList2);
    for (it = _intList.begin(); it != _intList.end(); ++it)
    {
        std::cout << *it << endl;   // 0, 1, 2, 3, 4, 100, 200, 300
    }

    // _intList2 数据已清空
    size = _intList2.size();
    std::cout << "size : "<< size << endl;   // size : 0

    std::cout << "" << endl;


    system("pause");
    return 0;
}

bool Is2(int i)
{
    return i == 2;
}

bool Sort(int i, int j)
{
    return i < j;
}

运行结果

这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值