Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Comments on When to use custom iterators versus pointers
Post
When to use custom iterators versus pointers
I am working on a toy project where I have a container for which I would like to write an iterator that iterates over the values in the container. Because the values are stored in a (c-style) array, this would look something like this:
class MyClass {
int _data[10] {};
public:
class MyIterator;
MyIterator begin();
MyIterator end();
};
class MyClass::MyIterator final {
int* _position {};
public:
using iterator_category = std::contiguous_iterator_tag;
using value_type = int;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
explicit MyIterator(int* from) : _position { from } {}
reference operator*() const { return *_position; }
pointer operator->() const { return _position; }
MyIterator& operator++() { ++_position; return *this; }
MyIterator operator++(int) { MyIterator tmp { *this }; operator++(); return tmp; }
MyIterator& operator--() { --_position; return *this; }
MyIterator operator--(int) { MyIterator tmp { *this }; operator--(); return tmp; }
MyIterator operator += (const difference_type& n) { _position += n; return *this; }
MyIterator operator -= (const difference_type& n) { _position -= n; return *this; }
MyIterator operator[](const difference_type& n) { return MyIterator(_position + n); }
friend auto operator<=>(MyIterator, MyIterator) = default;
friend auto operator+(MyIterator it, difference_type n) { MyIterator result { it }; result += n; return result; }
friend auto operator-(MyIterator it, difference_type n) { MyIterator result { it }; result -= n; return result; }
friend auto operator+(difference_type n, MyIterator it) { return it + n; }
};
MyClass::MyIterator MyClass::begin() {
return MyClass::MyIterator(&_data[0]);
}
MyClass::MyIterator MyClass::end() {
return MyClass::MyIterator(&_data[0] + 10);
}
However, while writing this code, I couldn't help but notice that I am essentially writing a wrapper around pointer arithmetic. Instead of writing a custom iterator, everything seems to work just as well when using the following, much more concise code:
class MyClass2 {
int _data[10] {};
public:
using MyIterator = int*;
MyIterator begin();
MyIterator end();
};
MyClass2::MyIterator MyClass2::begin() {
return &_data[0];
}
MyClass2::MyIterator MyClass2::end() {
return &_data[0] + 10;
}
I understand that using a raw pointer as an iterator is enabled by the specialisation of std::iterator_traits
. So why would/should I bother encapsulating the iterator in a full class with trivial code when using a raw pointer works just as well?
I do understand that there are scenarios where iterators require more complexity than plain pointer arithmetic. However, I suspect that most iterators can be written as iterating over an array. Is there any reason to wrap an iterator for array-like classes in a custom iterator class? If yes, is there any class in the standard library that implements this? I could only find std::iterator
, but this is deprecated and doesn't really implement anything...
2 comment threads