list 是什么?
list 本质是:双向链表
std::list<int> l = {1,2,3};
底层结构
nullptr <- [1] <-> [2] <-> [3] -> nullptr
特点:
- 不连续内存
- 每个节点单独分配(malloc/new)
list 的核心特性
插入/删除超级快
auto it = l.begin(); l.insert(it, 10); l.erase(it);
时间复杂度:O(1)
原因: 只需要改指针,不用移动数据
不支持随机访问
l[0]; // ❌ 不支持随机访问
只能:
auto it = l.begin(); ++it;
时间复杂度:O(n)
迭代器稳定
auto it = l.begin(); l.push_back(10); // it 仍然有效 ✅
原因: 每个节点独立,不会搬家!
list 常用接口
插入
l.push_back(10); l.push_front(5); l.insert(it, 100);
删除
l1.merge(l2);
l.pop_back(); l.pop_front(); l.(it); l.();

