【C++】std::list

std::list:

std::list 是 C++ 标准库中的一个双向链表容器,它是顺序容器中的一种。与其他顺序容器如 std::vector 和 std::deque 不同,std::list 在插入和删除操作上具有显著的优势,特别是在容器的中间部分。

特性

  1. 双向链表 : std::list 使用双向链表实现,每个元素包含指向前一个元素和后一个元素的指针。这使得在链表的任意位置进行插入和删除操作都具有 O(1) 的时间复杂度。

  2. 非随机访问 : std::list 不支持通过下标随机访问元素。只能通过迭代器进行访问,这意味着访问某个位置的元素需要 O(n) 时间。

  3. 动态大小: 容器大小可以动态调整。

  4. 稳定的迭代器 : 在对 std::list 进行插入和删除操作时,除了被删除的迭代器外,其余迭代器保持有效。

常用操作

1. 创建和初始化
cpp 复制代码
#include <list>
#include <iostream>

int main() {
    // 创建一个空的 list
    std::list<int> lst1;

    // 创建一个初始化为 5 个元素的 list,元素值为 10
    std::list<int> lst2(5, 10);

    // 使用初始化列表创建 list
    std::list<int> lst3 = {1, 2, 3, 4, 5};

    return 0;
}

2. 访问和修改元素

cpp 复制代码
cpp
#include <list>
#include <iostream>

int main() {
    std::list<int> lst = {1, 2, 3, 4, 5};

    // 使用迭代器遍历 list
    for (auto it = lst.begin(); it != lst.end(); ++it) {
        std::cout << *it << " ";
    }
    std::cout << std::endl;

    // 修改元素
    auto it = lst.begin();
    ++it; // 移动到第二个元素
    *it = 20;

    std::cout << "Modified list: ";
    for (const auto& elem : lst) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

3. 添加和删除元素

cpp 复制代码
#include <list>
#include <iostream>

int main() {
    std::list<int> lst = {1, 2, 3, 4, 5};

    // 在末尾添加元素
    lst.push_back(6);

    // 在前面添加元素
    lst.push_front(0);

    // 删除末尾元素
    lst.pop_back();

    // 删除前面元素
    lst.pop_front();

    std::cout << "List after push and pop operations: ";
    for (const auto& elem : lst) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}

4. 插入和删除指定位置的元素

cpp 复制代码
#include <list>
#include <iostream>

int main() {
    std::list<int> lst = {1, 2, 3, 4, 5};

    // 插入元素
    auto it = lst.begin();
    ++it; // 移动到第二个位置
    lst.insert(it, 10); // 在第二个位置插入 10

    // 删除指定位置的元素
    it = lst.begin();
    ++it; // 移动到第二个位置
    lst.erase(it); // 删除第二个位置的元素

    std::cout << "List after insert and erase operations: ";
    for (const auto& elem : lst) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}
5. 其他常用方法
  • size(): 返回 list 中元素的数量。
  • empty(): 判断 list 是否为空。
  • clear(): 清空 list 中的所有元素。
  • front(): 访问 list 的第一个元素。
  • back(): 访问 list 的最后一个元素。
  • resize(): 调整 list 的大小,多余的元素会被删除,不足的地方会使用默认构造函数初始化。

示例:

cpp 复制代码
#include <list>
#include <iostream>

int main() {
    std::list<int> lst = {1, 2, 3, 4, 5};

    // 输出前端和后端元素
    std::cout << "Front element: " << lst.front() << std::endl;
    std::cout << "Back element: " << lst.back() << std::endl;

    // 修改前端和后端元素
    lst.front() = 10;
    lst.back() = 50;

    std::cout << "Modified front element: " << lst.front() << std::endl;
    std::cout << "Modified back element: " << lst.back() << std::endl;

    // 调整大小
    lst.resize(8, 0); // 增加元素,并用 0 填充
    std::cout << "Size after resizing to 8: " << lst.size() << std::endl;

    return 0;
}
相关推荐
yiangL_14 小时前
课后作业4:基于 Python 的古诗词词云可视化(P205)
开发语言·python
JosieBook14 小时前
【鸿蒙开源】把 Windows 任务栏上的流量表搬到鸿蒙 PC:TrafficMonitor 1.86 的 C++ 内核移植实践
c++·windows·harmonyos
Huangjin007_15 小时前
【C++ 算法竞赛篇】STL 常用知识点速通指南
c++·算法
geovindu16 小时前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
skywalk816317 小时前
把「假成功」赶出工具层:lightharness 两天升级复盘(4×5 模式矩阵 + 工具层 5 个缺陷)
开发语言·前端·人工智能·lightharness
敲上瘾17 小时前
Qt控件通用属性:geometry、qrc资源到QSS样式表等
开发语言·c++·qt·ui
炸膛坦客17 小时前
单片机/C/C++八股:(三十三)取别名
c语言·c++·单片机
听情歌落俗18 小时前
嵌入式学习1-C语言基础语法与STM32实践
c语言·开发语言
小飞在努力18 小时前
【实现负载均匀在线判题⑤】MVC 架构搭建与 Model 题目数据模块实现
c语言·开发语言·c++·笔记·后端
ShineWinsu18 小时前
对于Qt:QFontDialog的解析
c++·qt·面试·gui·字体·客户端·qfontdialog