C++STL---list常见用法

C++ STL中的list

list是C++标准模板库(STL)中的一个序列容器,它实现了一个双向链表。与vectordeque相比,list支持快速的任意位置插入和删除操作,但不支持快速随机访问。

基本操作
  1. 创建和初始化

    #include <list>
    std::list<int> l1; // 创建一个空的list
    std::list<int> l2(5, 10); // 创建一个大小为5的list,每个元素初始化为10
    std::list<int> l3 = {1, 2, 3, 4, 5}; // 初始化列表
    
  2. 访问元素

    • front()back():访问第一个和最后一个元素。
    • list不支持operator[]at(),因为它不提供随机访问。
    int first = l3.front(); // 1
    int last = l3.back(); // 5
    
  3. 修改元素

    • push_back(value)push_front(value):在list的末尾或开头添加一个元素。
    • pop_back()pop_front():移除list的最后一个或第一个元素。
    • insert(position, value):在指定位置插入一个元素。
    • erase(position)erase(start, end):删除一个或多个元素。
    • clear():清空所有元素。
    l3.push_back(6); // l3: {1, 2, 3, 4, 5, 6}
    l3.push_front(0); // l3: {0, 1, 2, 3, 4, 5, 6}
    l3.pop_back(); // l3: {0, 1, 2, 3, 4, 5}
    l3.pop_front(); // l3: {1, 2, 3, 4, 5}
    auto it = l3.begin();
    std::advance(it, 2); // 移动迭代器到第三个元素
    l3.insert(it, 99); // l3: {1, 2, 99, 3, 4, 5}
    l3.erase(it); // l3: {1, 2, 3, 4, 5}
    
  4. 大小

    • size():返回当前元素的数量。
    • empty():检查容器是否为空。
    size_t num_elements = l3.size(); // 5
    bool is_empty = l3.empty(); // false
    
  5. 遍历

    • 使用迭代器进行遍历。
    for (int x : l3) std::cout << x << " ";
    for (auto it = l3.begin(); it != l3.end(); ++it) std::cout << *it << " ";
    
相关算法

list可以与STL中的算法库配合使用,但由于其内部结构是链表,某些基于随机访问的算法(如std::sort)不适用。不过,list提供了自己的成员函数来处理排序和其他操作。

  1. 排序

    • 使用list的成员函数sort(),因为标准的std::sort要求随机访问迭代器。
    l3.sort(); // 对list进行排序
    ``` 
    
    
  2. 反转

    • 使用list的成员函数reverse()来反转元素。
    l3.reverse(); // 反转list中的元素
    
  3. 查找

    auto it = std::find(l3.begin(), l3.end(), 3);
    if (it != l3.end()) {
        std::cout << "Element found: " << *it << std::endl;
    }
    
  4. 计数

    int count = std::count(l3.begin(), l3.end(), 3);
    
  5. 删除特定元素

    • 使用remove()成员函数直接删除所有匹配的元素。
    l3.remove(3); // 删除所有值为3的元素
    
  6. 遍历并执行操作

    std::for_each(l3.begin(), l3.end(), [](int& x){ x *= 2; });
    

list是一个非常有用的容器,特别是在需要频繁插入和删除操作时。它的灵活性使得在处理非连续存储的数据结构时非常高效。通过结合STL的算法,可以有效地管理和处理存储在list中的数据。

相关推荐

  1. C++STL---list常见

    2024-06-06 02:06:03       35 阅读
  2. mybatisPlus 常见

    2024-06-06 02:06:03       45 阅读
  3. JDBC常见

    2024-06-06 02:06:03       22 阅读
  4. [scala] 列表常见

    2024-06-06 02:06:03       55 阅读
  5. BOM 常见对话框

    2024-06-06 02:06:03       29 阅读
  6. C++ Lists(链表)基本

    2024-06-06 02:06:03       32 阅读
  7. Python中锁的常见

    2024-06-06 02:06:03       65 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-06-06 02:06:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-06-06 02:06:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-06-06 02:06:03       82 阅读
  4. Python语言-面向对象

    2024-06-06 02:06:03       91 阅读

热门阅读

  1. python中的预编译正则表达式

    2024-06-06 02:06:03       28 阅读
  2. 6_5 test

    6_5 test

    2024-06-06 02:06:03      23 阅读
  3. GNU Linux 下安装目录的规范

    2024-06-06 02:06:03       21 阅读
  4. 【C++】浅拷贝与深拷贝

    2024-06-06 02:06:03       34 阅读
  5. Leetcode 297. Serialize and Deserialize Binary Tree

    2024-06-06 02:06:03       28 阅读
  6. 视觉SLAM

    2024-06-06 02:06:03       25 阅读
  7. 003 Spring注解

    2024-06-06 02:06:03       18 阅读
  8. nuxt3 api如何透传(不引第3方库)

    2024-06-06 02:06:03       28 阅读