#include <iostream>
#include <list>
using namespace std;
int main()
{
list<int> li = { 1,2,3 };
li.push_front(4); //在 list开端进行插入
li.push_back(5);//在list末尾进行插入
auto it = find(li.begin(), li.end(), 3); // 返回指向元素3 的 iterator
li.insert(it, 6);// 在it 指向的位置前插入6 并且返回指向6 的iterator
it++; // 迭代器往后移一个位置
li.erase(it); //将迭代器所指的元素删除O(1) 时间 ,同时迭代器失效
list<int>li2 = { 7,8,9,10 };
auto it1 = li2.begin();
auto it2 = li2.end();
li.splice(it,li2,it1,it2); // 将li2中范围为it1, it2 的元素插入到li 中it 所指的位置后
system("pause");
return 0;
}
list 性质:
1 我们可以在任何位置对list 进行快速地插入和删除。
2 搜素速度O(n), 比vector慢, 因为vector 是顺序存储,有cache 缓存机制,而list 不是连续存储,cache效果没有vector 那么好。
3 不支持随机存储。
forward list 只支持单向访问。