目录
前言
List类的基本概念
List的构造函数
List类迭代器的使用
List的功能
List的元素访问
List与vector比较
前言
- vector的insert和erase都会导致迭代器失效
- list的insert不会导致迭代器失效,erase会导致迭代器失效

- insert导致失效的原因是开辟了新空间后,迭代器扔指向原空间
- erase导致失效的原因是销毁的空间不是连续的空间,迭代器找不到下一块小空间的位置
List类的基本概念
List类的文档:cplusplus.com/reference/list/list/?kw=list
1、list是在常数范围内的任意位置进行插入和删除的序列容器,可以向前向后双向迭代
2、list的底层是双向链表结构
3、list和forword_list相似,但后者是单链表,且只能向前迭代
4、和array、vector等容器相比,list可以在任意位置插入、删除元素,效率高
5、list的缺点是不支持随机访问、额外开辟空间存放结点信息、查找效率低需要遍历整个链表
List的构造函数
| 构造函数 |  
     接口说明 
     | 
|  
     list (size_type n, const value_type& val = value_type()) 
     |  
     构造的list中包含n个值为val的元素  
     | 
|  
     list()  
     |  
     构造空的list  
     | 
|  
     list (const list& x)  
     |  
     拷贝构造函数  
     | 
|  
     list (InputIterator first, InputIterator last)  
     |  
     用[first, last)区间中的元素构造list 
     | 
// list的构造
void TestList1()
{
    list<int> l1;                         //构造空的l1
    list<int> l2(4, 100);                 //l2中放4个值为100的元素
    list<int> l3(l2.begin(), l2.end());   //用l2的[begin(), end())左闭右开的区间构造l3
    list<int> l4(l3);                     //用l3拷贝构造l4
    //以数组为迭代器区间构造l5
    int array[] = { 16,2,77,29 };
    list<int> l5(array, array + sizeof(array) / sizeof(int));
    //列表格式初始化
    list<int> l6{ 1,2,3,4,5 };//C++11的initializer_list,多参数构造函数会发生隐式类型转换
    //用迭代器方式打印l5中的元素
    list<int>::iterator it = l5.begin();
    while (it != l5.end())
    {
        cout << *it << " ";
        ++it;
    }       
    cout << endl;
    // C++11范围for的方式遍历
    for (auto& e : l5)
        cout << e << " ";
    cout << endl;
}
List类迭代器的使用
| 函数声明 |  
     接口说明 
     | 
|  
     begin +  
      
     end 
     | 返回第一个元素的迭代器、返回最后一个元素下一个位置的迭代器 | 
|  
     rbegin +  
      
     rend 
     |  
     返回第一个元素的reverse_iterator,即end位置 
     (最后一个元素下一个位置的迭代器) 
     ,返回最后一个元素下一个位置的reverse_iterator,即begin位置 
     (第一个元素的迭代器) 
     | 
- begin和end为正向迭代器,对迭代器执行++操作,迭代器向后移动
- rbegin和rend为反向迭代器,对迭代器执行++操作,迭代器向前移动
// list迭代器的使用
// 注意:遍历链表只能用迭代器和范围for
void PrintList(const list<int>& l)
{
    // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
    for (list<int>::const_iterator it = l.begin(); it != l.end(); ++it)
    {
        cout << *it << " ";
        // *it = 10; 编译不通过
    }
    cout << endl;
}
void TestList2()
{
    int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
    list<int> l(array, array + sizeof(array) / sizeof(array[0]));
    // 使用正向迭代器正向list中的元素
    // list<int>::iterator it = l.begin();   // C++98中语法
    auto it = l.begin();                     // C++11之后推荐写法
    while (it != l.end())
    {
        cout << *it << " ";
        ++it;
    }
    cout << endl;
    // 使用反向迭代器逆向打印list中的元素
    // list<int>::reverse_iterator rit = l.rbegin();
    auto rit = l.rbegin();
    while (rit != l.rend())
    {
        cout << *rit << " ";
        ++rit;
    }
    cout << endl;
}List的功能
|  
     函数声明  
     | 接口说明 | 
|  
     empty 
     | 检测list是否为空,是返回true,否则返回false | 
| size | 返回list中有效节点的个数 | 
| push_front | 头插 | 
| pop_front | 头删 | 
| push_back | 尾插 | 
| pop_back | 尾删 | 
| insert | 指定位置插入 | 
| erase | 删除指定位置的元素 | 
| swap | 交换两个list中的元素 | 
| clear | 清空list中的有效元素 | 
// list插入和删除
// push_back/pop_back/push_front/pop_front
void TestList3()
{
    int array[] = { 1, 2, 3 };
    list<int> L(array, array + sizeof(array) / sizeof(array[0]));
    // 在list的尾部插入4,头部插入0
    L.push_back(4);
    L.push_front(0);
    PrintList(L);
    // 删除list尾部节点和头部节点
    L.pop_back();
    L.pop_front();
    PrintList(L);
}List的元素访问
|  
     函数声明  
     | 接口说明 | 
|  
     front 
     |  
     返回list的第一个节点中值的引用 
     | 
| back |  
     返回list的最后一个节点中值的引用  
     | 
List与vector比较
| vector | list | |
| 底层结构 |  
     动态顺序表,一段连续空间 
     |  
     带头结点的双向循环链表 
     | 
| 随机访问 |  
     支持随机访问,效率为O(1)  
     |  
     不支持随机访问, 
     效率为O(N) 
     | 
| 插入和删除 |  
     任意位置插入和删除效率低,需要搬移元素,时间复杂度为O(N),插入时有可能需要增容 
     (开辟新空间,拷贝元素,释放旧空间,导致效率更低) 
     |  
     任意位置插入和删除效率高,不 
     需要搬移元素,时间复杂度为  
     O(1) 
     | 
| 空间利用率 |  
     底层为连续空间,不容易造成内存碎片,空间利用率  
      
     高,缓存利用率高 
     |  
      底层节点动态开辟,小节点容易 
      造成内存碎片,空间利用率低, 
      缓存利用率低  
      | 
| 迭代器 |  
     原生指针 
     | 对原生指针(结点指针)进行封装 | 
| 迭代器失效 |  
     在插入元素时,要给所有的迭代器重新赋值,因为插入  
      
     元素有可能会导致重新扩容,致使原来迭代器失效,删 
     除时,当前迭代器需要重新赋值否则会失效  
     |  
     插入元素不会导致迭代器失效,  
     删除元素时,只会导致当前迭代 
     器失效,其他迭代器不受影响 
     | 
| 使用场景 |  
     需要高效存储,支持随机访问,不关心插入删除效率 
     |  
     大量插入和删除操作,不关心随 
     机访问 
     | 
~over~



















