For my own reference, and possibly others’, here’s a cookbook example of a custom C++ iterator type to use when the implementation details of a particular container is unimportant and the normal C++ algorithms should be used on iterator begin and end ranges.
class custom_iterator : public std::iterator<std::input_iterator_tag, exposed_type>
{
public:
custom_iterator(something_that_initializes_the_type_exposer& initializer)
: exposer_(initializer)
{
}
reference operator*()
{
return exposer_.ref();
}
const reference operator*() const
{
return exposer_.ref();
}
pointer operator->()
{
return exposer_.ptr();
}
const pointer operator->() const
{
return exposer_.ptr();
}
custom_iterator& operator++()
{
++it_;
return *this;
}
bool operator==(const custom_iterator& other) const
{
return it_ == other.it_;
}
bool operator!=(const custom_iterator& other) const
{
return !(*this == other);
}
private:
something_that_exposes_the_type exposer_;
};
