Reputation: 165
I have implemented a (non-const) random access iterator for my custom container class by inheriting from the very basic std::iterator template class (see below). All that was required to do this was to pass in the required types.
I have not been able to find much information on how to set up a reverse iterator, but I'm pretty sure there's a method where I can use the existing iterator with a few new typedefs and define rbegin() and rend(). And that's where I'm stuck. Could someone help?
template <class T, class A = std::allocator<T>>
class ring {
public:
typedef ring<T, A> self_type;
typedef T value_type;
typedef A alloc_type;
typedef ptrdiff_t size_type;
typedef typename alloc_type::difference_type difference_type;
typedef typename alloc_type::pointer pointer;
typedef typename alloc_type::reference reference;
typedef typename alloc_type::const_pointer const_pointer;
typedef typename alloc_type::const_reference const_reference;
class iterator; // we implement iterator as nested class, so no need to typedef it
iterator begin() { return iterator(this, 0); }
iterator end() { return iterator(this, size()); } // the current size (which is one past the end of the array)
class iterator : public std::iterator<std::random_access_iterator_tag, value_type, difference_type, pointer, reference> {
private:
self_type *ring_; // typedefs defined in ring class apply here
size_type offset_;
public:
iterator(self_type *r, size_type o) : ring_{r}, offset_{o} {}
reference operator* () const { return (*ring_)[offset_]; }
pointer operator-> () const { return &(operator*()); }
iterator& operator++ () {
++offset_;
return *this;
}
friend bool operator== (const iterator& it1, const iterator& it2) { return ((it1.ring_ == it2.ring_ && it1.offset_ == it2.offset_)); }
friend bool operator!= (const iterator& it1, const iterator& it2) { return (!(it1 == it2)); }
iterator& operator-- () {
--offset_;
return *this;
}
iterator operator++ (int) {
iterator clone(*this); // make a duplicate
++offset_;
return clone; // return the duplicate
}
iterator operator-- (int) { // has to be return by value
iterator clone(*this);
--offset_;
return clone;
}
iterator& operator+=(size_type n) {
offset_ += n;
return *this;
}
iterator& operator-=(size_type n) {
offset_ -= n;
return *this;
}
...
reference operator[] (size_type n) const { return (*ring_)[n]; }
};
};
Upvotes: 2
Views: 632
Reputation: 20619
You need to define the reverse_iterator
type alias and the rbegin
and rend
methods. The reverse iterator should be implemented with std::reverse_iterator
(defined in header <iterator>
)
using reverse_iterator = std::reverse_iterator< iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
reverse_iterator rbegin() { return end (); }
const_reverse_iterator rbegin() const { return end (); }
reverse_iterator rend () { return begin(); }
const_reverse_iterator rend () const { return begin(); }
const_reverse_iterator crbegin() const { return rbegin(); }
const_reverse_iterator crend () const { return rend (); }
And that's everything. No magic.
Upvotes: 3