当前位置:   article > 正文

迭代器

迭代器

迭代器概述

迭代器是一种抽象的设计概念,在设计模式中iterator模式被定义为:提供一种方法,可以按序访问某一聚合物(容器)所含元素,且不暴露该聚合物的内部表达方式。

在STL中,迭代器又起着将容器与算法联合到一起的作用。

  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. #include <deque>
  5. #include <algorithm>
  6. using namespace std;
  7. int main()
  8. {
  9. const int arr[7]{1, 2, 3, 4, 5, 6, 7};
  10. vector<int> varr(arr, arr + 7);
  11. list<int> larr(arr, arr + 7);
  12. deque<int> darr(arr, arr + 7);
  13. vector<int>::iterator it1 = find(varr.begin(), varr.end(), 3);
  14. list<int>::iterator it2 = find(larr.begin(), larr.end(), 4);
  15. deque<int>::iterator it2 = find(darr.begin(), darr.end(), 5);
  16. return 0;
  17. }

上例用法就是常用的迭代器使用方式。find算法在查找时,与具体的数据结构无关,只要给出待查找数据集合的范围,find就可在该范围中查找,找到返回该元素在区间中的位置,否则返回end。

所示的迭代器都是依附于容器创建、使用的,除此之外其实也可以设计一种泛型的迭代器。

迭代器实现原理

迭代器是一种行为类似指针的对象,因此迭代器实现上首先需要对" * " 和 " -> "进行重载。

我们以list为对象,设计一个迭代器:

  1. // list结构
  2. template <typename T>
  3. class List{
  4. void insert_front (T value);
  5. void insert_end (T value);
  6. void display (std::ostream &os = std::cout)const;
  7. private:
  8. ListItem<T>* _end;
  9. ListItem<T>* _front;
  10. long _size;
  11. };
  12. tempalte <typename T>
  13. class ListItem{
  14. public:
  15. T value() const {return _value; }
  16. LsitItem* next()const {return _next; }
  17. private:
  18. T _value;
  19. ListItem* _next;
  20. }

list底层结构为带头结点的双向循环链表,迭代器在移动时,只能按照链表的结构前后依次移动,因此链表的迭代器需要对原生态的指针进行封装,因为当对迭代器++时,应该通过节点中的next指针域找到下一个节点。 

  1. // 迭代器
  2. template<class T, class Ref, class Ptr>
  3. struct __list_iterator
  4. {
  5. typedef __list_iterator<T, T&, T*> iterator;
  6. typedef __list_iterator<T, const T&, const T*> const_iterator;
  7. typedef __list_iterator<T, Ref, Ptr> self;
  8. typedef bidirectional_iterator_tag iterator_category;
  9. typedef T value_type;
  10. typedef Ptr pointer;
  11. typedef Ref reference;
  12. typedef ptrdiff_t difference_type;
  13. typedef ListItem<T>* link_type;
  14. typedef size_t size_type;
  15. link_type node;
  16. __list_iterator(link_type x)
  17. : node(x)
  18. {}
  19. __list_iterator()
  20. {}
  21. __list_iterator(const iterator& x)
  22. : node(x.node)
  23. {}
  24. bool operator==(const self& x) const
  25. {
  26. return node == x.node;
  27. }
  28. bool operator!=(const self& x) const
  29. {
  30. return node != x.node;
  31. }
  32. reference operator*() const
  33. {
  34. return (*node).data;
  35. }
  36. pointer operator->() const
  37. {
  38. return &(operator*());
  39. }
  40. self& operator++(){
  41. node = (link_type)((*node).next);
  42. return *this;
  43. }
  44. self operator++(int)
  45. {
  46. self tmp = *this;
  47. ++*this;
  48. return tmp;
  49. }
  50. self& operator--()
  51. {
  52. node = (link_type)((*node).prev);
  53. return *this;
  54. }
  55. self operator--(int)
  56. {
  57. self tmp = *this;
  58. --*this;
  59. return tmp;
  60. }
  61. };

 迭代器与类的融合

1. 定义迭代器类


           2. 在容器类中统一迭代器名字

  1. // 如list:
  2. template <class T, class Alloc = alloc>
  3. class list
  4. {
  5. typedef __list_iterator<T, T&, T*> iterator;
  6. // ...
  7. };

3. 在容器类中添加获取迭代器范围的接口:

  1. template <class T, class Alloc = alloc>
  2. class list
  3. {
  4. iterator begin()
  5. {
  6. return (link_type)((*node).next);
  7. }
  8. iterator end()
  9. {
  10. return node;
  11. }
  12. // ...
  13. };

 

 

迭代器萃取参考:https://blog.csdn.net/LLZK_/article/details/53714015

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/179664
推荐阅读
相关标签
  

闽ICP备14008679号