• 【万字总结】C++——list的基本使用和模拟实现(建议收藏)


    目录

    一、list基本介绍

    二、list的使用

    1、list的初始化方式

    2、list的增删查改

    push_front和pop_front与push_back和pop_back

    insert

    erase

    3、list迭代器的使用

    正向迭代器

    反向迭代器

    4、list获取头尾元素

    5、list容量操作

    6、list的其他操作

    sort

    splice

    remove

    unique

    merge

    reverse

    assign

    swap

    7、结点的构造函数

    三、模拟迭代器类

    迭代器类的模板参数

    1、构造函数

    2、运算符重载

    ++

    ==

    !=

    *

    ->

    四、list的模拟实现

    1、构造函数

    2、拷贝构造函数

    3、赋值运算符重载函数

    4、析构函数

    5、迭代器相关函数

    begin和end

    insert

    erase

    push_back和pop_back与push_front和pop_front

    clear

    五、反向迭代器的模拟实现

    六、完整代码 

    reverse_iterator.h

    list.h

    七、vector与list优劣


    一、list基本介绍

    1. list是可以在常数范围内在任意位置进行插入和删除的序列式容器,并且该容器可以前后双向迭代。

    2. list的底层是双向链表结构,双向链表中每个元素存储在互不相关的独立节点中,在节点中通过指针指向 其前一个元素和后一个元素。

    3. list与forward_list非常相似:最主要的不同在于forward_list是单链表,只能朝前迭代,已让其更简单高 效。

    4. 与其他的序列式容器相比(array,vector,deque),list通常在任意位置进行插入、移除元素的执行效率 更好。

    5. 与其他序列式容器相比,list和forward_list最大的缺陷是不支持任意位置的随机访问,比如:要访问list 的第6个元素,必须从已知的位置(比如头部或者尾部)迭代到该位置,在这段位置上迭代需要线性的时间 开销;list还需要一些额外的空间,以保存每个节点的相关联信息(对于存储类型较小元素的大list来说这 可能是一个重要的因素)。

    二、list的使用

    1、list的初始化方式

    1. list<int> lt1;//构造空容器
    2. list<int> lt2(20, 2);//构造有20个2的int型容器
    3. list<int> lt3(lt2);//拷贝构造
    4. string str("hello world");
    5. //使用迭代器
    6. list<char> lt4(str.begin(), str.end());//使用区间给容器赋值
    7. int myints[] = {16,2,77,29};
    8. std::list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );//使用数组进行初始化

    2、list的增删查改

    push_front和pop_front与push_back和pop_back

    1. void test_list2()
    2. {
    3. list<int> lt;
    4. //尾插
    5. lt.push_back(1);
    6. lt.push_back(2);
    7. lt.push_back(3);
    8. lt.push_back(4);
    9. //1 2 3 4
    10. //尾删
    11. lt.pop_back();
    12. //1 2 3
    13. //头插
    14. lt.push_front(0);
    15. //0 1 2 3
    16. //头删
    17. lt.pop_front();
    18. //1 2 3
    19. //打印
    20. for (auto e : lt)
    21. {
    22. cout << e << " ";
    23. }
    24. cout << endl;
    25. //1 2 3
    26. }

    insert

    查阅文档我们可以发现list有三种插入方式:

    1、在指定迭代器位置插入一个数。

    2、在指定迭代器位置插入n个值为val的数。

    3、在指定迭代器位置插入一段迭代器区间。 

    1. list<int> lt;
    2. //尾插
    3. lt.push_back(1);
    4. lt.push_back(2);
    5. lt.push_back(3);
    6. lt.push_back(4);
    7. // 1 2 3 4
    8. //方式一
    9. //先确定2的位置
    10. list<int>::iterator pos = find(lt.begin(), lt.end(), 2);
    11. //在2的前面插入一个0
    12. lt.insert(pos, 0);
    13. // 1 0 2 3 4
    14. //方式二
    15. //在2的前面插入两个0
    16. lt.insert(pos, 2, 0);
    17. //1 0 0 0 2 3 4
    18. //方式三
    19. vector<int> v(3, 0);
    20. lt.insert(pos, v.begin(), v.end());
    21. //在2的前面插入三个0
    22. //1 0 0 0 0 0 0 2 3 4

    erase

     文档中记载有两种删除方式:

    1. 删除指定迭代器位置的元素。
    2. 删除指定迭代器区间(左闭右开)的所有元素。
    1. list<int> lt;
    2. //尾插
    3. lt.push_back(1);
    4. lt.push_back(2);
    5. lt.push_back(3);
    6. lt.push_back(4);
    7. // 1 2 3 4
    8. //
    9. //找到2的位置用迭代器指向它获得
    10. list<int>::iterator pos1 = find(lt.begin(), lt.end(),2);
    11. lt.erase(pos1);
    12. // 1 3 4
    13. list<int>::iterator pos2 = find(lt.begin(), lt.end(), 3);
    14. lt.erase(pos2, lt.end());
    15. // 1

    3、list迭代器的使用

    正向迭代器

    1. list<int> lt(3, 7);
    2. list<int>::iterator it = lt.begin();
    3. while (it != lt.end())
    4. {
    5. cout << *it << " ";
    6. it++;
    7. }
    8. //输出3个7

    反向迭代器

    1. list<int> lt;
    2. //尾插
    3. lt.push_back(1);
    4. lt.push_back(2);
    5. lt.push_back(3);
    6. lt.push_back(4);
    7. list<int>::reverse_iterator rit = lt.rbegin();
    8. while (rit != lt.rend())
    9. {
    10. cout << *rit << " ";
    11. rit++;
    12. }
    13. //4 3 2 1

    4、list获取头尾元素

    1. list<int> lt;
    2. //尾插
    3. lt.push_back(1);
    4. lt.push_back(2);
    5. lt.push_back(3);
    6. lt.push_back(4);
    7. //输出最后一个元素的值
    8. cout << lt.back() << endl;
    9. //输出第一个元素的值
    10. cout << lt.front() << endl;

    5、list容量操作

    1. list<int> lt;
    2. //尾插
    3. lt.push_back(1);
    4. lt.push_back(2);
    5. lt.push_back(3);
    6. lt.push_back(4);
    7. //输出数据个数
    8. cout << lt.size() << endl;
    9. //当所给值大于当前的size时,将size扩大到该值,扩大的数据为第二个所给值
    10. //若未给出,则默认为容器所存储类型的默认构造函数所构造出来的值。
    11. //当所给值小于当前的size时,将size缩小到该值。
    12. //容量增加到5
    13. lt.resize(5);
    14. //判断容器内是否为空
    15. if (lt.empty())
    16. {
    17. cout << "容器为空" << endl;
    18. }
    19. else
    20. {
    21. cout << "容器不为空" << endl;
    22. }
    23. //清空容器的数据,不改变容量大小
    24. lt.clear();
    25. cout << lt.size() << endl;

    6、list的其他操作

    sort

    将容器中的数据默认按照升序排列。

    1. list<int> lt;
    2. //尾插
    3. lt.push_back(3);
    4. lt.push_back(4);
    5. lt.push_back(1);
    6. lt.push_back(2);
    7. lt.sort();
    8. for (auto e : lt)
    9. {
    10. cout << e << " ";
    11. }
    12. cout << endl;
    13. //输出 1 2 3 4

    splice

    作用是拼接两个list容器。

    由文档可知有三种方式: 

    1、将一个容器拼接到另一个容器迭代器指向的位置。

    2、将容器中的某一个数据拼接到另一个容器迭代器指向的位置。

    3、将要容器中迭代器指向的区间的数据拼接到另一个容器迭代器指向的位置。

    1. //方式一
    2. list<int> lt1;
    3. //尾插
    4. lt1.push_back(1);
    5. lt1.push_back(2);
    6. lt1.push_back(3);
    7. lt1.push_back(4);
    8. list<int> lt2(3, 5);
    9. list<int>::iterator pos1 = find(lt1.begin(), lt1.end(), 2);
    10. lt1.splice(pos1, lt2);
    11. for (auto e : lt1)
    12. {
    13. cout << e << " ";
    14. }
    15. cout << endl;
    16. //1 5 5 5 2 3 4
    17. //方式二
    18. list<int> lt1;
    19. //尾插
    20. lt1.push_back(1);
    21. lt1.push_back(2);
    22. lt1.push_back(3);
    23. lt1.push_back(4);
    24. list<int> lt2(3, 5);
    25. list<int>::iterator pos1 = find(lt1.begin(), lt1.end(), 2);
    26. lt1.splice(pos1, lt2, lt2.begin());
    27. //1 5 2 3 4
    28. //方式三
    29. list<int> lt1;
    30. //尾插
    31. lt1.push_back(1);
    32. lt1.push_back(2);
    33. lt1.push_back(3);
    34. lt1.push_back(4);
    35. list<int> lt2(3, 5);
    36. list<int>::iterator pos1 = find(lt1.begin(), lt1.end(), 2);
    37. list<int>::iterator it2 = lt2.begin();
    38. lt1.splice(pos1, lt2, lt2.begin(), lt2.end());
    39. //1 5 5 5 2 3 4

    remove

    删除容器中的某个值

    1. list<int> lt1;
    2. //尾插
    3. lt1.push_back(1);
    4. lt1.push_back(2);
    5. lt1.push_back(3);
    6. lt1.push_back(3);
    7. lt1.push_back(3);
    8. lt1.push_back(3);
    9. lt1.push_back(4);
    10. lt1.remove(3);
    11. for (auto e : lt1)
    12. {
    13. cout << e << " ";
    14. }
    15. //1 2 4

    unique

    去除容器容器中连续重复的元素。

    1. list<int> lt;
    2. lt.push_back(1);
    3. lt.push_back(1);
    4. lt.push_back(1);
    5. lt.push_back(2);
    6. lt.push_back(2);
    7. lt.push_back(2);
    8. lt.push_back(3);
    9. lt.push_back(4);
    10. lt.push_back(4);
    11. lt.push_back(4);
    12. lt.push_back(4);
    13. lt.push_back(4);
    14. lt.unique();
    15. for (auto e : lt)
    16. {
    17. cout << e << " ";
    18. }
    19. //1 2 3 4

    merge

    类似归并排序,将一个有序容器归并到另一个有序容器中,使其还是有序的。

    1. list<int> lt1;
    2. lt1.push_back(1);
    3. lt1.push_back(3);
    4. lt1.push_back(5);
    5. lt1.push_back(7);
    6. list<int> lt2;
    7. lt2.push_back(2);
    8. lt2.push_back(4);
    9. lt2.push_back(6);
    10. lt2.push_back(8);
    11. lt1.merge(lt2);
    12. for (auto e : lt1)
    13. {
    14. cout << e << " ";
    15. }
    16. //1 2 3 4 5 6 7 8

    reverse

    将容器中的元素进行逆置。

    1. list<int> lt1;
    2. lt1.push_back(1);
    3. lt1.push_back(3);
    4. lt1.push_back(5);
    5. lt1.push_back(7);
    6. list<int> lt2;
    7. lt2.push_back(2);
    8. lt2.push_back(4);
    9. lt2.push_back(6);
    10. lt2.push_back(8);
    11. lt1.merge(lt2);
    12. //1 2 3 4 5 6 7 8
    13. lt1.reverse();
    14. for (auto e : lt1)
    15. {
    16. cout << e << " ";
    17. }
    18. //8 7 6 5 4 3 2 1

    assign

    将新内容分配给容器,并且会覆盖原容器中的值。

    由文档可知有两种方式:

    1、将n个val值分配给容器

    2、将迭代器区间中的内容分配给容器

    1. //方式一
    2. list<int> lt1;
    3. lt1.push_back(1);
    4. lt1.push_back(3);
    5. lt1.push_back(5);
    6. lt1.push_back(7);
    7. lt1.assign(3, 3);
    8. for (auto e : lt1)
    9. {
    10. cout << e << " ";
    11. }
    12. // 3 3 3
    13. //方式二
    14. list<int> lt1;
    15. lt1.push_back(1);
    16. lt1.push_back(3);
    17. lt1.push_back(5);
    18. lt1.push_back(7);
    19. list<int> lt2;
    20. lt2.push_back(2);
    21. lt2.push_back(4);
    22. lt2.push_back(6);
    23. lt2.push_back(8);
    24. lt1.assign(lt2.begin(), lt2.end());
    25. for (auto e : lt1)
    26. {
    27. cout << e << " ";
    28. }
    29. //2 4 6 8

    swap

    交换两个容器的内容

    1. list<int> lt1;
    2. lt1.push_back(1);
    3. lt1.push_back(3);
    4. lt1.push_back(5);
    5. lt1.push_back(7);
    6. list<int> lt2;
    7. lt2.push_back(2);
    8. lt2.push_back(4);
    9. lt2.push_back(6);
    10. lt2.push_back(8);
    11. lt1.swap(lt2);
    12. for (auto e : lt1)
    13. {
    14. cout << e << " ";
    15. }
    16. //2 4 6 8

    7、结点的构造函数

    构造一个结点,并且给结点的数据域he指针域初始化。

    1. template<class T>//类模板
    2. struct ListNode
    3. {
    4. ListNode* _next;
    5. ListNode* _prev;
    6. T _data;
    7. ListNode(const T& data = T())
    8. :_next(nullptr)
    9. , _prev(nullptr)
    10. , _data(data)
    11. {}

    三、模拟迭代器类

    为什么在vector和string中不用实现一个迭代器类呢?

    因为vector和string中的数据都是存储在一块连续的地址空间,我们直接使迭代器像原生指针那样解引用、自增、自减。就可以得到我们所要得到的效果。

    可是list却不是如此,我们知道list的本质是一个双向带头循环链表。地址空间并不连续,我们就不能单纯的像原生指针那样进行操作了。

    而迭代器的意义就是,让使用者可以不必关心容器的底层实现,可以用简单统一的方式对容器内的数据进行访问。

    迭代器类的模板参数

    1. //迭代器
    2. template<class T, class Ref, class Ptr>//类模板

    由以上代码我们可以看出来,迭代器类的模板参数列表有三个模板参数。

    这也与list模拟实现中的两个迭代器相对应。

    1. typedef _list_iterator iterator;
    2. typedef _list_iteratorconst T&, const T*> const_iterator;

    其具体作用其实就是为了让我们使用普通迭代器的时候,编译器实例化出一个普通迭代器对象。当我们使用const迭代器的时候就会实例化一个const迭代器对象。

    1、构造函数

    本质就是根据结点指针构造一个迭代器对象。

    1. //构造函数
    2. __list_iterator(Node* x)
    3. :_node(x)
    4. {}

    2、运算符重载

     注意:

    self是当前迭代器对象的类型

    		typedef __list_iterator self;
    

    ++

    本质是使结点指针指向后一个结点,并且返回下一个结点指针。

    1. //++it
    2. self& operator++()
    3. {
    4. _node = _node->_next;
    5. return *this;
    6. }
    7. //it++
    8. //切记后置++不能传引用
    9. //因为tmp是个临时对象,除了作用域会销毁的因此只能拷贝不能传引用
    10. self operator++(int)
    11. {
    12. self tmp(*this);
    13. //保存++之前的值以便于返回
    14. _node = _node->_next;
    15. return tmp;
    16. }

    注意: 切记后置不能传引用,因为tmp是个临时对象,出了作用域会销毁的因此只能拷贝不能传引用

    --

    本质是使结点值真指向前一个结点,并且返回上一个结点指针。

    1. //--it
    2. self& operator--()
    3. {
    4. _node = _node->_prev;
    5. return *this;
    6. }
    7. //it--
    8. self operator--(int)
    9. {
    10. self tmp(*this);
    11. //保存--之前的值以便于返回
    12. _node = _node->_prev;
    13. return tmp;
    14. }

    注意: 切记后置不能传引用,因为tmp是个临时对象,出了作用域会销毁的因此只能拷贝不能传引用 

    ==

    本质就是比较此时两个迭代器当中的结点指针是否指向同一个位置

    1. bool operator==(const self& it) const
    2. {
    3. return _node == it._node;
    4. }

    !=

    1. bool operator!=(const self& it) const
    2. {
    3. return _node != it._node;
    4. }

    *

    由于list是一个链表结构,*解引用实质就是返回此结点指针所指向的数据。

    1. //需要T&传T& 需要const T& 就传 const T&
    2. Ref operator*()//由于用的引用,所以可读可写
    3. {
    4. return _node->_data;
    5. }

    ->

    如果list容器中结点中存放的不是内置类型,而是自定义类型,那么我们就要用到->。

    比如下面的日期类:

    1. struct Date
    2. {
    3. int _year;
    4. int _month;
    5. int _day;
    6. Date(int year = 1, int month = 1, int day = 1)
    7. :_year(year)
    8. , _month(month)
    9. , _day(day)
    10. {}
    11. };
    12. void test_list2()
    13. {
    14. list lt;
    15. lt.push_back(Date(2022, 3, 12));
    16. lt.push_back(Date(2022, 3, 13));
    17. lt.push_back(Date(2022, 3, 14));
    18. //普通指针用解引用访问,结构体指针用箭头
    19. list::iterator it = lt.begin();
    20. while (it != lt.end())
    21. {
    22. //(*it)代表结点的数据也就是日期类的对象
    23. /*cout << (*it)._year << " ";
    24. cout << (*it)._month << " ";
    25. cout << (*it)._day << " ";
    26. cout << endl;*/
    27. cout << it->_year << " ";
    28. cout << it->_month << " ";
    29. cout << it->_day << " ";
    30. cout << endl;
    31. it++;
    32. }
    33. cout << endl;
    34. }

    那么该如何重载呢?

    在这里我们先写出重载的函数,再加以解释。

    1. Ptr operator->()
    2. {
    3. return &_node->_data;
    4. }

    如果你认真去推理你会发现这个重载函数是有问题的,it->找到的是自定义类型Date*,只有再->才是对应的Date的成员变量呀。所以按理说这里应该是it->->,才行呀。

    因此,我们可以得出,如果两个->的话,程序的可读性太差了,所以这里编译器做了特殊的处理,所以省略了一个->。

    四、list的模拟实现

    1、构造函数

    1. list()//构造函数
    2. {
    3. _head = new Node();//申请头结点
    4. _head->_next = _head;//头结点的后一个结点指向自己
    5. _head->_prev = _head;//头结点的前一个结点指向自己
    6. }

    2、拷贝构造函数

    传统写法:

    首先申请一个头结点,使头结点前驱指针和后继指针都指向自己。然后将容器中的数据通过遍历的方式尾插到新容器后面。

    现代写法:

    首先申请一个头结点,使头结点前驱指针和后继指针都指向自己。然后创建一个名叫tmp的list容器并且使lt1容器通过迭代器区间的方式初始化tmp,随后交换tmp的头结点和lt2。

    1. 传统写法
    2. //lt2(lt1)
    3. list(const list& lt)
    4. {
    5. _head = new Node();
    6. _head->_next = _head;
    7. _head->_prev = _head;
    8. for (auto e : lt)
    9. {
    10. push_back(e);
    11. }
    12. }
    13. //现代写法
    14. //lt2(lt1)
    15. list(const list<int>& lt)
    16. {
    17. _head = new Node();
    18. _head->_next = _head;
    19. _head->_prev = _head;
    20. list tmp(lt.begin(), lt.end());
    21. std::swap(_head, tmp._head);
    22. }

    3、赋值运算符重载函数

    传统写法:

    直接将lt中的值一个个遍历尾插到lt1中。

    现代写法:

    直接交换两个容器。

    首先利用编译器机制,故意不使用引用接收参数,通过编译器自动调用list的拷贝构造函数构造出来一个list对象,然后调用swap函数将原容器与该list对象进行交换即可。

    这样做相当于将应该用clear清理的数据,通过交换函数交给了容器lt,而当该赋值运算符重载函数调用结束时,容器lt会自动销毁,并调用其析构函数进行清理。

    1. //传统写法
    2. //lt2=lt1
    3. list& operator=(const list& lt)
    4. {
    5. if (this != <)
    6. {
    7. clear();
    8. for (auto e : lt)
    9. {
    10. push_back(e);
    11. }
    12. }
    13. return *this;
    14. }
    15. //现代写法
    16. list<int>& operator=(list lt)
    17. {
    18. std::swap(_head, lt._head);
    19. return *this;
    20. }

    4、析构函数

    先用clear清理数据,然后释放头结点,再把头结点置空。

    1. ~list()
    2. {
    3. clear();
    4. delete _head;
    5. _head = nullptr;
    6. }

    5、迭代器相关函数

    begin和end

    1. iterator begin()
    2. {
    3. return iterator(_head->_next);
    4. }
    5. iterator end()
    6. {
    7. return iterator(_head);
    8. }
    9. const_iterator begin() const
    10. {
    11. return const_iterator(_head->_next);
    12. }
    13. const_iterator end() const
    14. {
    15. return const_iterator(_head);
    16. }

    insert

    先根据所给迭代器得到该位置处的结点指针cur,然后通过cur指针找到前一个位置的结点指针prev,接着根据所给数据x构造一个待插入结点,之后再建立新结点与cur之间的双向关系,最后建立新结点与prev之间的双向关系即可。

    1. iterator insert(iterator pos, const T& x)
    2. {
    3. Node* cur = pos._node;
    4. Node* prev = cur->_prev;
    5. Node* newnode = new Node(x);
    6. prev->_next = newnode;
    7. newnode->_prev = prev;
    8. newnode->_next = cur;
    9. cur->_prev = newnode;
    10. return iterator(newnode);
    11. }

    erase

    首先使用断言assert来使所删除位置不能是头结点。先根据所给迭代器得到该位置处的结点指针cur,然后通过cur指针找到前一个位置的结点指针prev,以及后一个位置的结点指针next,紧接着释放cur结点,最后建立prev和next之间的双向关系即可。

    1. iterator erase(iterator pos)
    2. {
    3. assert(pos != end());
    4. Node* prev = pos._node->_prev;
    5. Node* next = pos._node->_next;
    6. delete pos._node;
    7. prev->_next = next;
    8. next->_prev = prev;
    9. return iterator(next);
    10. }

    push_back和pop_back与push_front和pop_front

    可以复用insert和erase进行处理。

    1. void push_back(const T& x)
    2. {
    3. //Node* tail = _head->_prev;//记录最后的尾结点
    4. //Node* newnode = new Node(x);//创建新结点
    5. 空节点也适用
    6. 链接各个节点之间的关系
    7. //tail->_next = newnode;
    8. //newnode->_prev = tail;
    9. //newnode->_next = _head;
    10. //_head->_prev = newnode;
    11. insert(end(), x);
    12. }
    13. void push_front(const T& x)
    14. {
    15. insert(begin(), x);
    16. }
    17. void pop_back()
    18. {
    19. erase(--end());
    20. }
    21. void pop_front()
    22. {
    23. erase(begin());
    24. }

    clear

    复用 erase,只保留头结点。

    1. void clear()
    2. {/*
    3. iterator it = begin();
    4. while (it != end())
    5. {
    6. iterator del = it++;
    7. delete del._node;
    8. }
    9. _head->_next = _head;
    10. _head->_prev = _head;*/
    11. iterator it = begin();
    12. while (it != end())
    13. {
    14. iterator del = it++;
    15. erase(del);
    16. }
    17. }

    五、反向迭代器的模拟实现

    反向迭代器有一点非常重要,就是rbegin和rend的位置。

    我们所知道的begin就是指向数据的第一个元素,然而end是指向最后一个元素的后一个位置。

    那么反向迭代器呢,rbegin是指向数据的最后一个数据,rend是指向数据的第一个数据的前一个。

    也就是如图所示的情况:

     可是,list的反向迭代器的源代码却不是这样实现的,而是如图所示:

     可是为什么这么做呢?,难道只是为了强迫症吗?

    其实不然,我们可以发现list这样做的话begin与rend和end和rbegin有很好的的对称性,也印证了为什么我们在vector和string的时候不对反向迭代器进行模拟实现,因为当我们模拟实现了list这个反向迭代器之后,我们的vector和string可以直接使用,就是因为这个很好的对称性!

    那么现在有一个问题,我们既然把rend和rbegin这样都向后移动了一位,那我们解引用的时候,也就是通过rend和rbegin拿到他们对应的值不就乱套了,所以我们此时对 *  运算符进行重载的时候很好的避免了这一点,这也就是为什么下边代码中 * 运算符重载返回的是 return *--prev;

    1. namespace mwb
    2. {
    3. //Iterator是哪个容器的迭代器,reverse_iterator就可以
    4. //适配出哪个容器的反向迭代器。复用的体现
    5. template <class Iterator, class Ref, class Ptr>
    6. class reverse_iterator
    7. {
    8. typedef reverse_iterator self;
    9. public:
    10. reverse_iterator(Iterator it)
    11. :_it(it)
    12. {}
    13. Ref operator*()
    14. {
    15. //return *_it;
    16. Iterator prev = _it;
    17. return *--prev;
    18. }
    19. Ptr operator->()
    20. {
    21. return &operator*();
    22. }
    23. self& operator++()
    24. {
    25. _it--;
    26. return *this;
    27. }
    28. self& operator--()
    29. {
    30. _it--;
    31. return *this;
    32. }
    33. bool operator!= (const self& rit) const
    34. {
    35. return _it != rit._it;
    36. }
    37. private:
    38. Iterator _it;
    39. };
    40. }

    六、完整代码 

    reverse_iterator.h

    1. #pragma once
    2. namespace mwb
    3. {
    4. //Iterator是哪个容器的迭代器,reverse_iterator就可以
    5. //适配出哪个容器的反向迭代器。复用的体现
    6. template <class Iterator, class Ref, class Ptr>
    7. class reverse_iterator
    8. {
    9. typedef reverse_iterator self;
    10. public:
    11. reverse_iterator(Iterator it)
    12. :_it(it)
    13. {}
    14. Ref operator*()
    15. {
    16. //return *_it;
    17. Iterator prev = _it;
    18. return *--prev;
    19. }
    20. Ptr operator->()
    21. {
    22. return &operator*();
    23. }
    24. self& operator++()
    25. {
    26. _it--;
    27. return *this;
    28. }
    29. self& operator--()
    30. {
    31. _it--;
    32. return *this;
    33. }
    34. bool operator!= (const self& rit) const
    35. {
    36. return _it != rit._it;
    37. }
    38. private:
    39. Iterator _it;
    40. };
    41. }

    list.h

    1. #pragma once
    2. #include"reverse_iterator.h"
    3. namespace mwb
    4. {
    5. template<class T>//类模板
    6. struct ListNode
    7. {
    8. ListNode* _next;
    9. ListNode* _prev;
    10. T _data;
    11. ListNode(const T& data = T())
    12. :_next(nullptr)
    13. , _prev(nullptr)
    14. , _data(data)
    15. {}
    16. };
    17. //迭代器
    18. template<class T, class Ref, class Ptr>//类模板
    19. struct __list_iterator//自定义类型
    20. {
    21. typedef ListNode Node;
    22. typedef __list_iterator self;
    23. //typedef __list_iterator
    24. Node* _node;
    25. //构造函数
    26. __list_iterator(Node* x)
    27. :_node(x)
    28. {}
    29. //it2=it1 浅拷贝
    30. //拷贝构造和赋值重载是否需要我们自己实现
    31. //析构呢?
    32. // 迭代器是借助节点的指针访问修改链表
    33. //节点属于链表,不属于迭代器,所以他不管释放
    34. //都不需要自己实现,默认生成的即可
    35. //需要T&传T& 需要const T& 就传 const T&
    36. Ref operator*()//由于用的引用,所以可读可写
    37. {
    38. return _node->_data;
    39. }
    40. Ptr operator->()
    41. {
    42. return &_node->_data;
    43. }
    44. //++it
    45. self& operator++()
    46. {
    47. _node = _node->_next;
    48. return *this;
    49. }
    50. //it++
    51. self operator++(int)
    52. {
    53. self tmp(*this);
    54. //保存++之前的值以便于返回
    55. _node = _node->_next;
    56. return tmp;
    57. }
    58. //--it
    59. self& operator--()
    60. {
    61. _node = _node->_prev;
    62. return *this;
    63. }
    64. //it--
    65. self operator--(int)
    66. {
    67. self tmp(*this);
    68. //保存--之前的值以便于返回
    69. _node = _node->_prev;
    70. return tmp;
    71. }
    72. bool operator!=(const self& it) const
    73. {
    74. return _node != it._node;
    75. }
    76. bool operator==(const self& it) const
    77. {
    78. return _node == it._node;
    79. }
    80. };
    81. template<class T>//类模板
    82. class list
    83. {
    84. typedef ListNode Node;
    85. public:
    86. typedef __list_iterator iterator;
    87. typedef __list_iteratorconst T&, const T*> const_iterator;
    88. typedef reverse_iteratorconst T&, const T*> const_reverse_iterator;
    89. typedef reverse_iterator reverse_iterator;
    90. reverse_iterator rbegin()
    91. {
    92. return reverse_iterator(end());
    93. }
    94. reverse_iterator rend()
    95. {
    96. return reverse_iterator(begin());
    97. }
    98. iterator begin()
    99. {
    100. return iterator(_head->_next);
    101. }
    102. iterator end()
    103. {
    104. return iterator(_head);
    105. }
    106. const_iterator begin() const
    107. {
    108. return const_iterator(_head->_next);
    109. }
    110. const_iterator end() const
    111. {
    112. return const_iterator(_head);
    113. }
    114. //
    115. list()
    116. {
    117. _head = new Node();
    118. _head->_next = _head;
    119. _head->_prev = _head;
    120. }
    121. list(size_t n, const T& val = T())
    122. {
    123. _head = new Node();
    124. _head->_next = _head;
    125. _head->_prev = _head;
    126. for (size_t i = 0; i < n; i++)
    127. {
    128. push_back(val);
    129. }
    130. }
    131. list(int n, const T& val = T())
    132. {
    133. _head = new Node();
    134. _head->_next = _head;
    135. _head->_prev = _head;
    136. for (int i = 0; i < n; i++)
    137. {
    138. push_back(val);
    139. }
    140. }
    141. template<class InputIterator>
    142. list(InputIterator first, InputIterator last)
    143. {
    144. _head = new Node();
    145. _head->_next = _head;
    146. _head->_prev = _head;
    147. while (first != last)
    148. {
    149. push_back(*first);
    150. first++;
    151. }
    152. }
    153. //现代写法
    154. //lt2(lt1)
    155. list(const list<int>& lt)
    156. {
    157. _head = new Node();
    158. _head->_next = _head;
    159. _head->_prev = _head;
    160. list tmp(lt.begin(), lt.end());
    161. std::swap(_head, tmp._head);
    162. }
    163. list<int>& operator=(list lt)
    164. {
    165. std::swap(_head, lt._head);
    166. return *this;
    167. }
    168. //传统写法
    169. lt2(lt1)
    170. //list(const list& lt)
    171. //{
    172. // _head = new Node();
    173. // _head->_next = _head;
    174. // _head->_prev = _head;
    175. // for (auto e : lt)
    176. // {
    177. // push_back(e);
    178. // }
    179. //}
    180. lt2=lt1
    181. //list& operator=(const list& lt)
    182. //{
    183. // if (this != <)
    184. // {
    185. // clear();
    186. // for (auto e : lt)
    187. // {
    188. // push_back(e);
    189. // }
    190. // }
    191. // return *this;
    192. //}
    193. ~list()
    194. {
    195. clear();
    196. delete _head;
    197. _head = nullptr;
    198. }
    199. void clear()
    200. {/*
    201. iterator it = begin();
    202. while (it != end())
    203. {
    204. iterator del = it++;
    205. delete del._node;
    206. }
    207. _head->_next = _head;
    208. _head->_prev = _head;*/
    209. iterator it = begin();
    210. while (it != end())
    211. {
    212. iterator del = it++;
    213. erase(del);
    214. }
    215. }
    216. void push_back(const T& x)
    217. {
    218. //Node* tail = _head->_prev;//记录最后的尾结点
    219. //Node* newnode = new Node(x);//创建新结点
    220. 空节点也适用
    221. 链接各个节点之间的关系
    222. //tail->_next = newnode;
    223. //newnode->_prev = tail;
    224. //newnode->_next = _head;
    225. //_head->_prev = newnode;
    226. insert(end(), x);
    227. }
    228. void push_front(const T& x)
    229. {
    230. insert(begin(), x);
    231. }
    232. iterator insert(iterator pos, const T& x)
    233. {
    234. Node* cur = pos._node;
    235. Node* prev = cur->_prev;
    236. Node* newnode = new Node(x);
    237. prev->_next = newnode;
    238. newnode->_prev = prev;
    239. newnode->_next = cur;
    240. cur->_prev = newnode;
    241. return iterator(newnode);
    242. }
    243. void pop_back()
    244. {
    245. erase(--end());
    246. }
    247. void pop_front()
    248. {
    249. erase(begin());
    250. }
    251. iterator erase(iterator pos)
    252. {
    253. assert(pos != end());
    254. Node* prev = pos._node->_prev;
    255. Node* next = pos._node->_next;
    256. delete pos._node;
    257. prev->_next = next;
    258. next->_prev = prev;
    259. return iterator(next);
    260. }
    261. private:
    262. Node* _head;
    263. };
    264. //void print_list(const list& lt)
    265. //{
    266. // list::iterator it = lt.begin();
    267. // while (it != lt.end())
    268. // {
    269. // cout << *it << " ";
    270. // it++;
    271. // }
    272. // cout << endl;
    273. //}
    274. void test_list1()
    275. {
    276. list<int> lt;
    277. lt.push_back(1);
    278. lt.push_back(2);
    279. lt.push_back(3);
    280. lt.push_back(4);
    281. list<int>::iterator it = lt.begin();
    282. while (it != lt.end())
    283. {
    284. *it *= 2;
    285. cout << *it << " ";
    286. it++;
    287. }
    288. cout << endl;
    289. }
    290. struct Date
    291. {
    292. int _year;
    293. int _month;
    294. int _day;
    295. Date(int year = 1, int month = 1, int day = 1)
    296. :_year(year)
    297. , _month(month)
    298. , _day(day)
    299. {}
    300. };
    301. void test_list2()
    302. {
    303. list lt;
    304. lt.push_back(Date(2022, 3, 12));
    305. lt.push_back(Date(2022, 3, 13));
    306. lt.push_back(Date(2022, 3, 14));
    307. //普通指针用解引用访问,结构体指针用箭头
    308. list::iterator it = lt.begin();
    309. while (it != lt.end())
    310. {
    311. //(*it)代表结点的数据也就是日期类的对象
    312. /*cout << (*it)._year << " ";
    313. cout << (*it)._month << " ";
    314. cout << (*it)._day << " ";
    315. cout << endl;*/
    316. cout << it->_year << " ";
    317. cout << it->_month << " ";
    318. cout << it->_day << " ";
    319. cout << endl;
    320. it++;
    321. }
    322. cout << endl;
    323. }
    324. void test_list3()
    325. {
    326. list<int> lt1;
    327. lt1.push_back(1);
    328. lt1.push_back(2);
    329. lt1.push_back(3);
    330. list<int> lt2(lt1);
    331. for (auto e : lt2)
    332. {
    333. cout << e << " ";
    334. }
    335. cout << endl;
    336. list<int> lt3;
    337. lt3.push_back(10);
    338. lt3.push_back(10);
    339. lt3.push_back(10);
    340. lt3.push_back(10);
    341. lt1 = lt3;
    342. for (auto e : lt1)
    343. {
    344. cout << e << " ";
    345. }
    346. cout << endl;
    347. }
    348. void test_list4()
    349. {
    350. list lt1(5, Date(2022, 3, 15));
    351. for (auto e : lt1)
    352. {
    353. cout << e._year << "/" << e._month << "/" << e._day << endl;
    354. }
    355. cout << endl;
    356. list<int> lt2(5, 1);
    357. for (auto e : lt2)
    358. {
    359. cout << e << " ";
    360. }
    361. cout << endl;
    362. }
    363. void test_list5()
    364. {
    365. list<int> lt1;
    366. lt1.push_back(1);
    367. lt1.push_back(2);
    368. lt1.push_back(3);
    369. lt1.push_back(4);
    370. list<int>::iterator it = lt1.begin();
    371. while (it != lt1.end())
    372. {
    373. //*it *= 2; // 修改
    374. cout << *it << " ";
    375. ++it;
    376. }
    377. cout << endl;
    378. list<int>::reverse_iterator rit = lt1.rbegin();
    379. while (rit != lt1.rend())
    380. {
    381. cout << *rit << " ";
    382. ++rit;
    383. }
    384. cout << endl;
    385. }
    386. }

    七、vector与list优劣

    vector缺陷:

    连续的物理空间,是优势也是劣势。

    劣势:

    1、空间不够要增容,增容代价比较大。

    2、可能存在一定空间浪费。按需申请,会导致频繁增容,所以一般都会2倍左右扩容。

    3、头部或者中部插入删除需要挪动数据,效率低下。

    list优势:

    1、按需申请释放空间

    2、list任意位置支持O(1)插入删除。

    综上所述:

    本质vector和list是互补的两个数据结构。

  • 相关阅读:
    人工智能、深度学习、机器学习常见面试题321~324
    C++入门学习笔记
    零基础学韩语-看韩剧追欧巴
    SpringBoot整合Mybatis-Plus
    6.判断是不是闰年
    基于PLC控制的门禁系统设计【附源码】
    JavaScript使用Ajax
    Azkaban使用
    TMP451
    HTTP缓存小结
  • 原文地址:https://blog.csdn.net/m0_57249790/article/details/127637591