Class reverse_iterator

Synopsis

#include <include/EASTL/iterator.h>

template <typename Iterator>
class reverse_iterator : public iterator<typename eastl::iterator_traits<Iterator>::iterator_category,
										 typename eastl::iterator_traits<Iterator>::value_type,
										 typename eastl::iterator_traits<Iterator>::difference_type,
										 typename eastl::iterator_traits<Iterator>::pointer,
										 typename eastl::iterator_traits<Iterator>::reference>

Description

reverse_iterator

From the C++ standard: Bidirectional and random access iterators have corresponding reverse iterator adaptors that iterate through the data structure in the opposite direction. They have the same signatures as the corresponding iterators. The fundamental relation between a reverse iterator and its corresponding iterator i is established by the identity: &*(reverse_iterator(i)) == &*(i - 1). This mapping is dictated by the fact that while there is always a pointer past the end of an array, there might not be a valid pointer before the beginning of an array.

Mentioned in

Inheritance

Ancestors: iterator

Methods

reverse_iterator overload
base
operator*
operator+
operator++ overload
operator+=
operator-
operator-- overload
operator-=
operator->
operator=This operator= isn't in the standard, but the the C++ library working group has tentatively approved it, as it allows const and non-const reverse_iterators to interoperate.
operator[]http://cplusplus.github.io/LWG/lwg-defects.html#386, http://llvm.org/bugs/show_bug.cgi?id=17883 random_access_iterator operator[] is merely required to return something convertible to reference

Source

Lines 218-307 in include/EASTL/iterator.h. Line 219 in include/EASTL/iterator.h.

template <typename Iterator>
class reverse_iterator : public iterator<typename eastl::iterator_traits<Iterator>::iterator_category,
                                         typename eastl::iterator_traits<Iterator>::value_type,
                                         typename eastl::iterator_traits<Iterator>::difference_type,
                                         typename eastl::iterator_traits<Iterator>::pointer,
                                         typename eastl::iterator_traits<Iterator>::reference>
{
public:
    typedef Iterator                                                   iterator_type;
    typedef iterator_type                                              wrapped_iterator_type;   // This is not in the C++ Standard; it's used by use to identify it as a wrapping iterator type.
    typedef typename eastl::iterator_traits<Iterator>::pointer         pointer;
    typedef typename eastl::iterator_traits<Iterator>::reference       reference;
    typedef typename eastl::iterator_traits<Iterator>::difference_type difference_type;
protected:
    Iterator mIterator;
public:
    EA_CPP14_CONSTEXPR reverse_iterator()      // It's important that we construct mIterator, because if Iterator
        : mIterator() { }   // is a pointer, there's a difference between doing it and not.
    EA_CPP14_CONSTEXPR explicit reverse_iterator(iterator_type i)
        : mIterator(i) { }
    EA_CPP14_CONSTEXPR reverse_iterator(const reverse_iterator& ri)
        : mIterator(ri.mIterator) { }
    template <typename U>
    EA_CPP14_CONSTEXPR reverse_iterator(const reverse_iterator<U>& ri)
        : mIterator(ri.base()) { }
    // This operator= isn't in the standard, but the the C++
    // library working group has tentatively approved it, as it
    // allows const and non-const reverse_iterators to interoperate.
    template <typename U>
    EA_CPP14_CONSTEXPR reverse_iterator<Iterator>& operator=(const reverse_iterator<U>& ri)
        { mIterator = ri.base(); return *this; }
    EA_CPP14_CONSTEXPR iterator_type base() const
        { return mIterator; }
    EA_CPP14_CONSTEXPR reference operator*() const
    {
        iterator_type i(mIterator);
        return *--i;
    }
    EA_CPP14_CONSTEXPR pointer operator->() const
        { return &(operator*()); }
    EA_CPP14_CONSTEXPR reverse_iterator& operator++()
        { --mIterator; return *this; }
    EA_CPP14_CONSTEXPR reverse_iterator operator++(int)
    {
        reverse_iterator ri(*this);
        --mIterator;
        return ri;
    }
    EA_CPP14_CONSTEXPR reverse_iterator& operator--()
        { ++mIterator; return *this; }
    EA_CPP14_CONSTEXPR reverse_iterator operator--(int)
    {
        reverse_iterator ri(*this);
        ++mIterator;
        return ri;
    }
    EA_CPP14_CONSTEXPR reverse_iterator operator+(difference_type n) const
        { return reverse_iterator(mIterator - n); }
    EA_CPP14_CONSTEXPR reverse_iterator& operator+=(difference_type n)
        { mIterator -= n; return *this; }
    EA_CPP14_CONSTEXPR reverse_iterator operator-(difference_type n) const
        { return reverse_iterator(mIterator + n); }
    EA_CPP14_CONSTEXPR reverse_iterator& operator-=(difference_type n)
        { mIterator += n; return *this; }
    // http://cplusplus.github.io/LWG/lwg-defects.html#386,
    // http://llvm.org/bugs/show_bug.cgi?id=17883
    // random_access_iterator operator[] is merely required to return something convertible to reference.
    // reverse_iterator operator[] can't necessarily know what to return as the underlying iterator
    // operator[] may return something other than reference.
    EA_CPP14_CONSTEXPR reference operator[](difference_type n) const
        { return mIterator[-n - 1]; }
};





Add Discussion as Guest

Log in