STL容器中 list(双向链表)的增删改查

list(双向链表)

std::list 是 C++ 标准模板库(STL)中的一个容器,它实现了一个双向链表。双向链表中的每个元素都包含三个部分:存储数据的元素本身、指向前一个元素的指针(或迭代器)、以及指向后一个元素的指针(或迭代器)。这使得在链表的任何位置进行插入和删除操作都非常高效,因为这些操作只需要改变指针的指向,而不需要移动其他元素。然而,与数组或 std::vector 相比,随机访问元素的速度较慢,因为访问元素需要从头或尾开始遍历链表。list支持快速的插入和删除操作,但不支持随机访问。

复制代码
#include <iostream>  
#include <list>  
  
int main() {  
    std::list<int> lst;  
  
    // 向list中添加元素  
    lst.push_back(10);  
    lst.push_front(5);  
  
    // 遍历list  
    for (std::list<int>::iterator it = lst.begin(); it != lst.end(); ++it) {  
        std::cout << "元素: " << *it << std::endl;  
    }  
  
    // 使用范围for循环遍历list(需要C++11及以后,并包含<initializer_list>)  
    // 注意:直接的范围for循环不适用于std::list,但可以使用其他方式模拟  
  
    return 0;  
}  
  
// 注意:由于list不支持随机访问迭代器,因此不能直接使用范围for循环。  
// 可以通过C++11的auto和基于范围的for循环来遍历list,但通常需要额外的辅助,如std::begin和std::end。

插入元素

向 std::list 中插入元素可以使用 insert 方法。它可以在指定位置之前插入一个新元素,也可以插入一个元素范围。

复制代码
#include <iostream>  
#include <list>  
  
int main() {  
    std::list<int> myList = {1, 2, 4};  
  
    // 在第二个元素之前插入3  
    auto it = myList.begin();  
    std::advance(it, 1); // 移动迭代器到第二个元素  
    myList.insert(it, 3);  
  
    // 遍历并打印  
    for (int elem : myList) {  
        std::cout << elem << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}

删除元素

删除元素可以使用 erase 方法,该方法删除迭代器指向的元素,并返回指向下一个元素的迭代器。

复制代码
#include <iostream>  
#include <list>  
  
int main() {  
    std::list<int> myList = {1, 2, 3, 4};  
  
    // 删除值为3的元素  
    for (auto it = myList.begin(); it != myList.end(); ) {  
        if (*it == 3) {  
            it = myList.erase(it); // 注意,这里迭代器需要更新  
        } else {  
            ++it;  
        }  
    }  
  
    // 遍历并打印  
    for (int elem : myList) {  
        std::cout << elem << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}

修改元素

修改元素非常简单,直接通过迭代器访问并修改其值。

复制代码
#include <iostream>  
#include <list>  
  
int main() {  
    std::list<int> myList = {1, 2, 3, 4};  
  
    // 修改第二个元素为10  
    auto it = myList.begin();  
    std::advance(it, 1); // 移动到第二个元素  
    *it = 10;  
  
    // 遍历并打印  
    for (int elem : myList) {  
        std::cout << elem << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}

遍历元素

遍历 std::list 可以使用范围基于的for循环(如上面示例所示),或者使用迭代器手动遍历。

复制代码
#include <iostream>  
#include <list>  
  
int main() {  
    std::list<int> myList = {1, 2, 3, 4};  
  
    // 使用迭代器遍历  
    for (auto it = myList.begin(); it != myList.end(); ++it) {  
        std::cout << *it << " ";  
    }  
    std::cout << std::endl;  
  
    return 0;  
}
相关推荐
I_belong_to_jesus16 小时前
C++关于分支预测的优化1
开发语言·c++·算法
Rabitebla17 小时前
【Linux】进程控制(十二) 自定义 Shell(二):内建命令与进程工作目录管理
linux·c++·笔记·学习·bash
码客-ygluu1 天前
一个通用游戏数据服务器
c++·架构
another heaven1 天前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
YLCHUP1 天前
【思维&图论建模】题解:债权(debt)_图论_建模_思路转化_思维题目_C++算法竞赛
c++·笔记·算法·数学建模·贪心算法·深度优先·图论
xiaoye-duck1 天前
《C++ 项目:手写大模型接入 SDK》会话模块整合:内存 SessionManager 与 DataManager 持久化联动
开发语言·数据库·c++·语言模型
思麟呀1 天前
背包DP(01背包+完全背包)
c++·算法
旖旎夜光1 天前
【C++ 基础入门】C++11 新特性(中):右值引用、移动语义与完美转发
开发语言·c++·笔记·学习
ShineWinsu1 天前
对于Qt:QTabWidget常见属性的解析
开发语言·数据库·c++·qt·浏览器·qtabwidget·标签页
Pniubi1 天前
力扣24. 两两交换链表中的节点(递归)
算法·leetcode·链表