code_junkie

Is there a dereference_iterator in the STL?

Nov 12th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.40 KB | None | 0 0
  1. #include <vector>
  2. #include <iterator>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. vector<int*> vec;
  10.  
  11. int i = 1;
  12. int j = 2;
  13. int k = 3;
  14.  
  15. vec.push_back(&i);
  16. vec.push_back(&j);
  17. vec.push_back(&k);
  18.  
  19. copy(deref_iterator(vec.begin()),
  20. deref_iterator(vec.end()),
  21. ostream_iterator<int>(cout, " ")); // prints "1 2 3"
  22.  
  23. return 0;
  24. }
  25.  
  26. #include <boost/ptr_container/ptr_vector.hpp>
  27. #include <iostream>
  28. #include <iterator>
  29. #include <algorithm>
  30.  
  31. using namespace std;
  32.  
  33. int main()
  34. {
  35. boost::ptr_vector<int> vec;
  36.  
  37. vec.push_back(new int(1));
  38. vec.push_back(new int(2));
  39. vec.push_back(new int(3));
  40.  
  41. copy(vec.begin(),vec.end(),
  42. ostream_iterator<int>(std::cout, " ")); // prints "1 2 3 "
  43.  
  44. return 0;
  45. }
  46.  
  47. #include <iterator>
  48.  
  49. template <typename T>
  50. struct PointedType;
  51.  
  52. template <typename T>
  53. struct PointedType<T*>
  54. {
  55. typedef T value_type;
  56. };
  57.  
  58. template <typename InputIterator>
  59. struct DerefIterator
  60. {
  61. typedef input_iterator_tag iterator_category;
  62. typedef typename PointedType<
  63. typename iterator_traits<InputIterator>::value_type>::value_type
  64. value_type;
  65. typedef typename iterator_traits<InputIterator>::difference_type
  66. difference_type;
  67. typedef value_type* pointer;
  68. typedef value_type& reference;
  69.  
  70. public:
  71. explicit DerefIterator(const InputIterator& ii)
  72. : it(ii) {}
  73.  
  74. // Returns the object pointed by the object referenced by it
  75. reference operator*() const { return **it; }
  76. pointer operator->() const { return *it; }
  77.  
  78. DerefIterator& operator++()
  79. {
  80. ++it;
  81. return *this;
  82. }
  83.  
  84. DerefIterator operator++(int)
  85. {
  86. DerefIterator tmp = *this;
  87. ++it;
  88. return tmp;
  89. }
  90.  
  91. bool equals(const DerefIterator<InputIterator> & di) const
  92. {
  93. return di.it == it;
  94. }
  95.  
  96. private:
  97. InputIterator it;
  98. };
  99.  
  100. // Equality functions
  101.  
  102. template <typename InputIterator>
  103. inline bool operator==(const DerefIterator<InputIterator>& di1,
  104. const DerefIterator<InputIterator>& di2)
  105. {
  106. return di1.equals(di2);
  107. }
  108.  
  109. template <typename InputIterator>
  110. inline bool operator!=(const DerefIterator<InputIterator>& di1,
  111. const DerefIterator<InputIterator>& di2)
  112. {
  113. return ! (di1 == di2);
  114. }
  115.  
  116. //Helper function
  117.  
  118. template <typename InputIterator>
  119. DerefIterator<InputIterator> deref_iterator(const InputIterator& ii)
  120. {
  121. return DerefIterator<InputIterator>(ii);
  122. }
Add Comment
Please, Sign In to add comment