当前位置: 首页 > news >正文

C++中std::list的使用详解和综合实战代码示例

std::list 是 C++ STL(标准模板库)中提供的一个 双向链表容器,适用于需要频繁在中间或头尾插入/删除元素的场景。


一、std::list 概述

#include <list>
  • 定义

    std::list<int> myList;
    
  • 底层结构:双向链表

  • 特点

    • 插入/删除效率高(O(1))
    • 不能随机访问(没有 [] 操作符,不能用索引)
    • 每个节点存储数据 + 前后指针

二、常见用法示例

1. 创建与初始化

std::list<int> list1;                           // 空列表
std::list<int> list2 = {1, 2, 3, 4};            // 列表初始化
std::list<int> list3(5, 10);                    // 5 个值为 10 的元素

2. 插入与删除

std::list<int> myList = {1, 2, 3};// 头尾插入
myList.push_front(0);  // -> 0 1 2 3
myList.push_back(4);   // -> 0 1 2 3 4// 头尾删除
myList.pop_front();    // -> 1 2 3 4
myList.pop_back();     // -> 1 2 3// 插入到指定位置(使用 iterator)
auto it = myList.begin();
std::advance(it, 1);          // 移动到第2个元素
myList.insert(it, 10);        // -> 1 10 2 3// 删除指定位置
it = myList.begin();
std::advance(it, 2);
myList.erase(it);             // -> 1 10 3

3. 遍历 list

for (auto it = myList.begin(); it != myList.end(); ++it)std::cout << *it << " ";for (const auto& val : myList)std::cout << val << " ";

4. 其他常用操作

myList.clear();              // 清空
myList.empty();              // 是否为空
myList.size();               // 元素个数
myList.front();              // 第一个元素
myList.back();               // 最后一个元素

三、特殊操作

1. 去重(相邻元素)

std::list<int> lst = {1, 1, 2, 2, 3, 3};
lst.unique();  // -> 1 2 3

2. 排序

lst.sort();     // 升序排序
lst.sort(std::greater<int>());  // 降序排序

3. 反转

lst.reverse();

四、自定义类型示例

struct Person {std::string name;int age;
};std::list<Person> people = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 35}
};people.sort([](const Person& a, const Person& b) {return a.age < b.age;
});

五、使用场景总结

需求是否适合 std::list
快速在中间插入/删除✅ 非常适合
随机访问元素❌ 不适合(用 vector 更合适)
大量头部插入
需要排序和去重
多线程场景⚠️ 需要加锁(非线程安全)

六、高级应用示例

下面是 std::list高级用法示范,涵盖:

  • 自定义排序与 splice
  • 使用 remove_if 删除特定条件元素
  • merge 合并已排序链表
  • 使用 unique 去重
  • 自定义类型支持 std::list

示例1:高级用法综合示范

#include <iostream>
#include <list>
#include <algorithm>
#include <string>struct Person {std::string name;int age;// 支持排序的比较函数bool operator<(const Person& other) const {return age < other.age;}// 支持去重的等价比较函数bool operator==(const Person& other) const {return name == other.name && age == other.age;}
};void PrintList(const std::list<Person>& lst, const std::string& title) {std::cout << title << ":\n";for (const auto& p : lst) {std::cout << "  " << p.name << ", age " << p.age << "\n";}std::cout << std::endl;
}int main() {std::list<Person> group1 = { {"Alice", 30}, {"Bob", 25}, {"Charlie", 28} };std::list<Person> group2 = { {"Diana", 26}, {"Eve", 24}, {"Bob", 25} };PrintList(group1, "Group 1");PrintList(group2, "Group 2");//  sort + merge(合并两个已排序 list)group1.sort();  // 按 age 升序排序group2.sort();group1.merge(group2);  // group2 被清空PrintList(group1, "Merged Group");//  unique 去重(需要 operator==)group1.unique();PrintList(group1, "After unique (去重)");//  remove_if:删除年龄小于 28 的成员group1.remove_if([](const Person& p) {return p.age < 28;});PrintList(group1, "After remove_if (age < 28)");//  splice:将一个 list 插入另一个位置std::list<Person> newcomers = { {"Frank", 31}, {"Grace", 29} };auto it = std::next(group1.begin());group1.splice(it, newcomers);  // 插入 newcomers 到 group1 中间PrintList(group1, "After splice newcomers");return 0;
}

输出:

Group 1:Alice, age 30Bob, age 25Charlie, age 28Group 2:Diana, age 26Eve, age 24Bob, age 25Merged Group:Eve, age 24Bob, age 25Bob, age 25Diana, age 26Charlie, age 28Alice, age 30After unique (去重):Eve, age 24Bob, age 25Diana, age 26Charlie, age 28Alice, age 30After remove_if (age < 28):Charlie, age 28Alice, age 30After splice newcomers:Charlie, age 28Frank, age 31Grace, age 29Alice, age 30

小结:std::list 高级用法速查

功能函数说明
排序list.sort()内部比较或自定义排序
合并list.merge()需先排序,合并后自动排序
去重list.unique()删除相邻重复元素(需支持 ==
条件删除remove_if()按 lambda 或谓词删除
插入链表splice()整段迁移插入,不拷贝、零成本
自定义类型支持重载 <==支持排序、去重、自定义谓词

示例2:实现简易任务调度系统

示例支持功能:

  • 添加任务(支持优先级)
  • 动态执行并删除任务
  • 删除过期任务
  • 支持按优先级排序执行

Task 定义:

#include <iostream>
#include <list>
#include <string>
#include <chrono>
#include <functional>
#include <algorithm>using Clock = std::chrono::steady_clock;struct Task {std::string name;int priority;  // 数字越大,优先级越高Clock::time_point expireTime;std::function<void()> action;bool operator<(const Task& other) const {return priority > other.priority;  // 用于 sort:高优先级在前}
};

TaskScheduler 类:

class TaskScheduler {
private:std::list<Task> tasks;public:void AddTask(const Task& task) {tasks.push_back(task);}void RemoveExpiredTasks() {auto now = Clock::now();tasks.remove_if([&](const Task& task) {return now > task.expireTime;});}void RunTopTask() {if (tasks.empty()) return;// 优先级排序(高优先级排前面)tasks.sort();auto task = tasks.front();tasks.pop_front();std::cout << "[Running] " << task.name << std::endl;task.action();}void RunAllTasks() {tasks.sort();for (const auto& task : tasks) {std::cout << "[Running] " << task.name << std::endl;task.action();}tasks.clear();}void PrintTasks() const {std::cout << "Scheduled Tasks:\n";for (const auto& task : tasks) {std::cout << "  " << task.name << " (priority: " << task.priority << ")\n";}}
};

使用示例

int main() {TaskScheduler scheduler;scheduler.AddTask({"TaskA", 1, Clock::now() + std::chrono::seconds(10),[] { std::cout << " -> Running TaskA\n"; }});scheduler.AddTask({"TaskB", 5, Clock::now() + std::chrono::seconds(5),[] { std::cout << " -> Running TaskB\n"; }});scheduler.AddTask({"TaskC", 3, Clock::now() + std::chrono::seconds(1),[] { std::cout << " -> Running TaskC\n"; }});std::cout << "All tasks:\n";scheduler.PrintTasks();std::this_thread::sleep_for(std::chrono::seconds(2));  // 模拟延时scheduler.RemoveExpiredTasks();std::cout << "\nTasks after removing expired:\n";scheduler.PrintTasks();std::cout << "\nExecuting tasks...\n";scheduler.RunAllTasks();
}

输出示例

All tasks:
Scheduled Tasks:TaskA (priority: 1)TaskB (priority: 5)TaskC (priority: 3)Tasks after removing expired:
Scheduled Tasks:TaskA (priority: 1)TaskB (priority: 5)Executing tasks...
[Running] TaskB-> Running TaskB
[Running] TaskA-> Running TaskA

优势

  • std::list 支持 任意位置插入/删除,无需重新分配内存。
  • 适合构建 实时任务队列异步处理池调度表系统
  • 支持 splice() 快速合并多个任务队列,零开销。

小结

操作时间复杂度
插入/删除(任意位置)O(1)(已知 iterator)
访问第 n 个元素O(n)
遍历所有元素O(n)

http://www.dtcms.com/a/295605.html

相关文章:

  • Linux进程间通信:管道机制全方位解读
  • uniapp转微信程序点击事件报错Error: Component “xx“ does not have a method “xx“解决方案
  • Linux724 逻辑卷挂载;挂载点扩容;逻辑卷开机自启
  • 【PZ-ZU7EV-KFB】——ZYNQ UltraScale + ZU7EV开发板ARM/FPGA异构计算开发平台,赋能多域智能硬件创新
  • The Missing Semester of Your CS Education 学习笔记以及一些拓展知识(六)
  • 从“类”到“道”——Python 面向对象编程全景解析
  • J2EE模式---组合实体模式
  • 从指标定义到AI执行流:衡石SENSE 6.0的BI PaaS如何重构ISV分析链路
  • 【推荐100个unity插件】Animator 的替代品?—— Animancer Pro插件的使用介绍
  • Mac电脑使用IDEA启动服务后,报service异常
  • 微算法科技(NASDAQ: MLGO)研究量子信息递归优化(QIRO)算法,为组合优化问题拓展解决新思路
  • 橱柜铰链的革命:炬森精密如何以创新科技重塑家居体验
  • 详解力扣高频SQL50题之197. 上升的温度【简单】
  • 重构数据库未来:金仓数据库,抢占 AI 原生时代先机
  • 数据结构系列之红黑树
  • 亚马逊云科技:以云为翼,助你翱翔数字新天空
  • pycharm配conda环境
  • 2025年PostgreSQL 详细安装教程(windows)
  • Pycharm、Python安装及配置小白教程
  • 智能制造场景195个术语的16个分类
  • 模块化商城的快速部署之道:ZKmall开源商城如何让电商功能即插即用
  • Unity VS Unreal Engine ,“电影像游戏的时代” 新手如何抉择引擎?(1)
  • Java设计模式-适配器模式
  • vue 中什么场景使用 export default 和setup()
  • Baumer工业相机堡盟工业相机如何通过YoloV8深度学习模型实现卫星图像识别(C#代码,UI界面版)
  • [数据结构]#6 树
  • Apache Commons:Java开发者的瑞士军刀
  • 【C++】使用箱线图算法剔除数据样本中的异常值
  • n8n AI资讯聚合与分发自动化教程:从数据获取到微信与Notion集成
  • 环特生物荣获“广西科学技术进步二等奖”