码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • list的const迭代器的实现


    要实现const迭代器首先要实现普通迭代器,普通迭代器主要是要实现他的解引用、后置加加等操作符,他的底层是指针,但是不是原生指针,是通过用一个类进行封装,通过对类进行传参数来解决问题,先定义链表的结点

    1. template<class T>
    2. struct __list_node
    3. {
    4. __list_node* _prev;
    5. __list_node* _next;
    6. T _val;
    7. __list_node(const T& x)
    8. :_prev(nullptr)
    9. , _next(nullptr)
    10. , _val(x)
    11. {}
    12. __list_node()
    13. :_prev(nullptr)
    14. , _next(nullptr)
    15. {}
    16. };

    接下来是实现list类

    1. template<class T>
    2. class list
    3. {
    4. typedef __list_node Node;
    5. public:
    6. typedef __list_iterator iterator;
    7. typedef __list_iteratorconst T&, const T*> const_iterator;
    8. list()
    9. {
    10. _head = new Node;
    11. _head->_prev = _head;
    12. _head->_next = _head;
    13. }
    14. ~list()
    15. {
    16. clear();
    17. delete _head;
    18. _head = nullptr;
    19. }
    20. iterator begin()
    21. {
    22. return iterator(_head->_next);
    23. }
    24. iterator end()
    25. {
    26. return iterator(_head);
    27. }
    28. const_iterator begin()const
    29. {
    30. return const_iterator(_head->_next);
    31. }
    32. const_iterator end()const
    33. {
    34. return const_iterator(_head);
    35. }
    36. private:
    37. Node* _head;
    38. };

    在这里面我用了三个模板参数去传递,这为之后定义const迭代器埋下了伏笔,接下来实现普通迭代器

    1. template<class T,class Ref,class Ptr>
    2. struct __list_iterator
    3. {
    4. typedef __list_node Node;
    5. typedef __list_iterator Self;
    6. Node* _node;
    7. __list_iterator(Node* node)
    8. :_node(node)
    9. {}
    10. //*it
    11. Ref operator*()
    12. {
    13. return _node->_val;
    14. }
    15. //++it
    16. Self& operator++()
    17. {
    18. _node = _node->_next;
    19. return *this;
    20. }
    21. //it++
    22. Self operator++(int)
    23. {
    24. Self tmp = *this;
    25. //_node = _node->_next;
    26. ++(*this);
    27. return tmp;
    28. }
    29. Self& operator--()
    30. {
    31. _node = _node->_prev;
    32. return *this;
    33. }
    34. Self operator--(int)
    35. {
    36. Self tmp = *this;
    37. //_node = _node->_prev;
    38. --(*this);
    39. return tmp;
    40. }
    41. Ptr operator->()
    42. {
    43. return &_node->_val;
    44. }
    45. bool operator!=(const Self& it)
    46. {
    47. return _node != it._node;
    48. }
    49. };

    在这个迭代器类中,我们定义了三个模板参数,其中第一个是为了传递list存放数据的类型,接下来两个是为了定义接下来的const迭代器在通过list代码中我们可以知道const迭代器传了两个const对象过去,通过传const对象去获得const迭代器,并且通过返回值来是返回的也是const对象,这就是const迭代器的封装类。

  • 相关阅读:
    『德不孤』Pytest框架 — 13、Pytest中Fixture装饰器(三)
    计算机毕业设计【HTML+CSS+JavaScript服装购物商城】毕业论文源码
    【Numpy总结】第六节:Numpy 元素的遍历
    Java基础入门·对存储文件File的相关操作
    torchvision.datasets.ImageFolder前的数据整理及使用方法
    MFC 模态对话框退出机制的探究
    spring-boot-starter-validation数据校验全局异常拦截处理
    计算机网络从三层交换机到路由器ospf
    【SLAM论文笔记】PL-EVIO笔记(下)
    PMP每日一练 | 考试不迷路-11.04(包含敏捷+多选)
  • 原文地址:https://blog.csdn.net/2301_77312705/article/details/134367789
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号