list从0到1的突破

[复制链接]
发表于 2026-1-20 18:41:16 | 显示全部楼层 |阅读模式
目次

媒介
1.list的先容
2.list的常见接口
2.1 构造函数( (constructor))  +接口分析    
2.2 list iterator 的利用
 2.3 list capacity
2.4 list element access
2.5 list modifiers
3.list的迭代器失效

附整套练习源码
竣事语


媒介

   前面我们学习了vector,本节我们将对新的容器list举行拆分学习,而且有了string和vector的底子,list容器的方法学习起来就会轻松很多。
  1.list的先容


    1. list是可以在常数范围内在恣意位置举行插入和删除的序列式容器,而且该容器可从前后双向迭代。
  2. list的底层是双向链表布局,双向链表中每个元素存储在互不干系的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。
  3. list与forward_list非常相似:最紧张的差别在于forward_list是单链表,只能朝前迭代,已让其更简单高 效。
  4. 与其他的序列式容器相比(array,vector,deque),list通常在恣意位置举行插入、移除元素的实行服从更好。
  5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持恣意位置的随机访问,好比:要访问list 的第6个元素,必须从已知的位置(好比头部大概尾部)迭代到该位置,在这段位置上迭代须要线性的时间 开销;list还须要一些额外的空间,以生存每个节点的干系联信息(对于存储范例较小元素的大list来说这大概是一个紧张的因素)
  

2.list的常见接口

2.1 构造函数( (constructor))  +接口分析    

   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
  1. void test1() {
  2.         list<int>l1;
  3.         list<int>l2(5, 10);
  4.         list<int>l3(l2.begin(), l2.end());//迭代器构造
  5.         list<int>l4(l2);//拷贝构造
  6.         //以数组区间迭代器构造list
  7.         float arr[] = { 5.20,13.14,9.99,8.88 };
  8.         list<float>l5(arr, arr + sizeof(arr) / sizeof(float));
  9.         // 列表格式初始化C++11
  10.         list<int> l6{ 1,2,3,4,5 };
  11.         // 用迭代器方式打印l5中的元素
  12.         list<float> ::iterator it = l5.begin();
  13.         while (it != l5.end()) {
  14.                 cout << *it << " ";
  15.                 it++;
  16.         }
  17.         cout << endl;
  18.         // C++11范围for的方式遍历
  19.         for (auto e : l6) {
  20.                 cout << e << " ";
  21.         }
  22. }
复制代码
注意:遍历链表只能用迭代器和范围for
2.2 list iterator 的利用


  1. void TestList2()
  2. {
  3.     int array[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
  4.     list<int> l(array, array + sizeof(array) / sizeof(array[0]));
  5.     // 使用正向迭代器正向list中的元素
  6.     // list<int>::iterator it = l.begin();   // C++98中语法
  7.     auto it = l.begin();                     // C++11之后推荐写法
  8.     while (it != l.end())
  9.     {
  10.         cout << *it << " ";
  11.         ++it;
  12.     }
  13.     cout << endl;
  14.     // 使用反向迭代器逆向打印list中的元素
  15.     // list<int>::reverse_iterator rit = l.rbegin();
  16.     auto rit = l.rbegin();
  17.     while (rit != l.rend())
  18.     {
  19.         cout << *rit << " ";
  20.         ++rit;
  21.     }
  22.     cout << endl;
  23. }
复制代码

跟vector险些是一样的
注意:
1. begin与end为正向迭代器,对迭代器实行++操纵,迭代器向后移动
2. rbegin(end)与rend(begin)为反向迭代器,对迭代器实行++操纵,迭代器向前移动 
 2.3 list capacity


2.4 list element access

 官方测试代码展示

  1. list<int> mylist;
  2. mylist.push_back(10);
  3. while (mylist.back() != 0)
  4. {
  5.         mylist.push_back(mylist.back() - 1);
  6. }
  7. cout << "mylist contains:";
  8. for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)
  9.         std::cout << ' ' << *it;
  10. cout << '\n';
复制代码
 

2.5 list modifiers

   push_front    在list首元素前插入值为val的元素       pop_front    删除list中第一个元素 
  push_back     在list尾部插入值为val的元素             pop_back删除list中末了一个元素 insert    在list position 位置中插入值为val的元素   erase删除list position位置的元素
  swap互换两个list中的元素                                        clear清空list中的有效元素 
  1. void print_list(const list<int>& ml) {
  2.         // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
  3.         list<int>::const_iterator it = ml.begin();
  4.         while (it != ml.end()) {
  5.                 cout << *it << " ";
  6.                 it++;
  7.         }
  8.         cout << endl;
  9. }
  10. void TestList3() {
  11.         list<int>mylist{ 1,2,3,4,5 };
  12.         mylist.push_back(6);
  13.         mylist.push_front(0);
  14.         print_list(mylist);
  15.         mylist.pop_back();
  16.         mylist.pop_front();
  17.         print_list(mylist);
  18. }
  19. void TestList4()
  20. {
  21.         int array1[] = { 1, 2, 3 };
  22.         list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  23.         // 获取链表中第二个节点
  24.         //auto pos = ++L.begin();
  25.         list<int>::iterator pos = ++L.begin();
  26.         cout << *pos << endl;
  27.         // 在pos前插入值为4的元素
  28.         L.insert(pos, 4);
  29.         print_list(L);
  30.         // 在pos前插入5个值为5的元素
  31.         L.insert(pos, 5, 5);
  32.         print_list(L);
  33.         // 在pos前插入[v.begin(), v.end)区间中的元素
  34.         vector<int> v{ 7, 8, 9 };
  35.         L.insert(pos, v.begin(), v.end());
  36.         print_list(L);
  37.         // 删除pos位置上的元素
  38.         L.erase(pos);
  39.         print_list(L);
  40.         // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
  41.         L.erase(L.begin(), L.end());
  42.         print_list(L);
  43. }
复制代码
 这里我们设置了一个打印链表值的函数,方便打印链表,只是打印整数,想打印其他值可以参考vector创建一个模版打印函数,让编译器本身推测打印数据的范例。
  1. template <class Container>
  2. void print(const Container& v) {
  3.         auto it = v.begin();
  4.         while (it != v.end()) {
  5.                 cout << *it << " ";
  6.                 it++;
  7.         }
  8.         cout << endl;
  9. }
复制代码
  1. void TestList5()
  2. {
  3.         // 用数组来构造list
  4.         int array1[] = { 1, 2, 3 ,4 ,5};
  5.         list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  6.         print_list(l1);
  7.         list<int>l2{ 6,7,8,9,10 };
  8.         // 交换l1和l2中的元素
  9.         l1.swap(l2);
  10.         print_list(l1);
  11.         print_list(l2);
  12.         // 将l2中的元素清空
  13.         l2.clear();
  14.         cout << l2.size() << endl;
  15. }
复制代码

3.list的迭代器失效

此处可将迭代器临时明确成雷同于指针,迭代器失效即迭代器所指向的节点的无效,即该节 点被删除了。由于list的底层布局为带头结点的双向循环链表,因此在list中举行插入时是不会导致list的迭代器失效的,只有在删除时才会失效,而且失效的只是指向被删除节点的迭代器,其他迭代器不会受到影响。
  1. void TestList() {
  2.         int arr[] = { 1,2,3,4,5,6,7,8,9 };
  3.         list<int>l1 (arr, arr + sizeof(arr) / sizeof(arr[0]));
  4.         auto it = l1.begin();
  5.         while (it != l1.end()) {
  6.                 l1.erase(it);
  7.                 it++;
  8.         }
  9. }
复制代码

修改后:
  1. void TestList() {
  2.         int arr[] = { 1,2,3,4,5,6,7,8,9 };
  3.         list<int>l1 (arr, arr + sizeof(arr) / sizeof(arr[0]));
  4.         print(l1);
  5.         auto it = l1.begin();
  6.         while (it != l1.end()) {
  7.                 //等价于l1.erase(it++);
  8.                 it = l1.erase(it);
  9.                
  10.         }
  11.         print(l1);
  12. }
复制代码
 变式删除偶数:
  1. void TestList1() {
  2.         int arr[] = { 1,2,3,4,5,6,7,8,9 };
  3.         list<int>l1(arr, arr + sizeof(arr) / sizeof(arr[0]));
  4.         print(l1);
  5.         auto it = l1.begin();
  6.         while (it != l1.end()) {
  7.                 if(*it%2==0)
  8.                 it = l1.erase(it);
  9.                 else
  10.                 it++;
  11.         }
  12.         print(l1);
  13. }
复制代码



附整套练习源码

  1. #define _CRT_SECURE_NO_WARNINGS#include <iostream>#include <list>#include <vector>using namespace std;void test1() {        list<int>l1;        list<int>l2(5, 10);        list<int>l3(l2.begin(), l2.end());//迭代器构造        list<int>l4(l2);//拷贝构造        //以数组区间迭代器构造list        float arr[] = { 5.20,13.14,9.99,8.88 };        list<float>l5(arr, arr + sizeof(arr) / sizeof(float));        // 列表格式初始化C++11        list<int> l6{ 1,2,3,4,5 };        // 用迭代器方式打印l5中的元素        list<float> ::iterator it = l5.begin();        while (it != l5.end()) {                cout << *it << " ";                it++;        }        cout << endl;        // C++11范围for的方式遍历        for (auto e : l6) {                cout << e << " ";        }        cout << endl;        cout << l5.size() << 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;}void test3() {                list<int> mylist;                mylist.push_back(10);                while (mylist.back() != 0)                {                        mylist.push_back(mylist.back() - 1);                }                cout << "mylist contains:";                for (list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it)                        std::cout << ' ' << *it;                cout << '\n';}template <class Container>
  2. void print(const Container& v) {
  3.         auto it = v.begin();
  4.         while (it != v.end()) {
  5.                 cout << *it << " ";
  6.                 it++;
  7.         }
  8.         cout << endl;
  9. }void print_list(const list<int>& ml) {
  10.         // 注意这里调用的是list的 begin() const,返回list的const_iterator对象
  11.         list<int>::const_iterator it = ml.begin();
  12.         while (it != ml.end()) {
  13.                 cout << *it << " ";
  14.                 it++;
  15.         }
  16.         cout << endl;
  17. }
  18. void TestList3() {
  19.         list<int>mylist{ 1,2,3,4,5 };
  20.         mylist.push_back(6);
  21.         mylist.push_front(0);
  22.         print_list(mylist);
  23.         mylist.pop_back();
  24.         mylist.pop_front();
  25.         print_list(mylist);
  26. }
  27. void TestList4()
  28. {
  29.         int array1[] = { 1, 2, 3 };
  30.         list<int> L(array1, array1 + sizeof(array1) / sizeof(array1[0]));
  31.         // 获取链表中第二个节点
  32.         //auto pos = ++L.begin();
  33.         list<int>::iterator pos = ++L.begin();
  34.         cout << *pos << endl;
  35.         // 在pos前插入值为4的元素
  36.         L.insert(pos, 4);
  37.         print_list(L);
  38.         // 在pos前插入5个值为5的元素
  39.         L.insert(pos, 5, 5);
  40.         print_list(L);
  41.         // 在pos前插入[v.begin(), v.end)区间中的元素
  42.         vector<int> v{ 7, 8, 9 };
  43.         L.insert(pos, v.begin(), v.end());
  44.         print_list(L);
  45.         // 删除pos位置上的元素
  46.         L.erase(pos);
  47.         print_list(L);
  48.         // 删除list中[begin, end)区间中的元素,即删除list中的所有元素
  49.         L.erase(L.begin(), L.end());
  50.         print_list(L);
  51. }void TestList5(){        // 用数组来构造list        int array1[] = { 1, 2, 3 ,4 ,5};        list<int> l1(array1, array1 + sizeof(array1) / sizeof(array1[0]));        //print_list(l1);        print(l1);        list<int>l2{ 6,7,8,9,10 };        // 互换l1和l2中的元素        l1.swap(l2);        //print_list(l1);        //print_list(l2);        print(l1);        print(l2);        // 将l2中的元素清空        l2.clear();        cout << l2.size() << endl;}void TestList() {
  52.         int arr[] = { 1,2,3,4,5,6,7,8,9 };
  53.         list<int>l1 (arr, arr + sizeof(arr) / sizeof(arr[0]));
  54.         print(l1);
  55.         auto it = l1.begin();
  56.         while (it != l1.end()) {
  57.                 //等价于l1.erase(it++);
  58.                 it = l1.erase(it);
  59.                
  60.         }
  61.         print(l1);
  62. }void TestList1() {
  63.         int arr[] = { 1,2,3,4,5,6,7,8,9 };
  64.         list<int>l1(arr, arr + sizeof(arr) / sizeof(arr[0]));
  65.         print(l1);
  66.         auto it = l1.begin();
  67.         while (it != l1.end()) {
  68.                 if(*it%2==0)
  69.                 it = l1.erase(it);
  70.                 else
  71.                 it++;
  72.         }
  73.         print(l1);
  74. }int main() {        //test3();        TestList1();        return 0;}
复制代码
竣事语

   本节内容就到此竣事啦,信赖各人对list有了进一步的相识,下节我们将一步一步实现本身的list!
  末了感谢各位友友的支持,给小编点个赞吧!!! 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!qidao123.com:ToB企服之家,中国第一个企服评测及软件市场,开放入驻,技术点评得现金

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

×
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表