Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for(auto i : c) -- Is there a short way to do it in reverse direction?

Tags:

c++

I have a custom container class and defined iterators, so I can do:

for (auto i : c)

but is there something to iterate in reverse direction?

Something like:

for_reverse (auto i : c)
like image 830
George Kourtis Avatar asked Feb 02 '14 11:02

George Kourtis


2 Answers

  1. You can use boost:

    #include <boost/range/adaptor/reversed.hpp>
    
    using namespace boost::adaptors;
    for (auto i : c | reversed)
        ...
    

    Or if you don't like operator overloading:

    #include <boost/range/adaptor/reversed.hpp>
    
    using namespace boost::adaptors;
    for (auto i : reverse(c))
        ...
    
  2. You can define a similar helper function using std::reverse_iterator so you don't depend on boost:

    template<typename It>
    class Range
    {
        It b, e;
    public:
        Range(It b, It e) : b(b), e(e) {}
        It begin() const { return b; }
        It end() const { return e; }
    };
    
    template<typename ORange, typename OIt = decltype(std::begin(std::declval<ORange>())), typename It = std::reverse_iterator<OIt>>
    Range<It> reverse(ORange && originalRange) {
        return Range<It>(It(std::end(originalRange)), It(std::begin(originalRange)));
    }
    

    Then you can write:

    for (auto i : reverse(c))
        ...
    

    Example

like image 179
leemes Avatar answered Oct 21 '22 00:10

leemes


A one-off pain

namespace details {
    template <class T> struct _reversed { T& t; _reversed(T& _t): t(_t) {} };
}

template <class T> details::_reversed<T> reversed(T& t) { return details::_reversed<T>(t); }
template <class T> details::_reversed<T const> reversed(T const& t) { return details::_reversed<T const>(t); }

namespace std {
    template <class T> auto begin(details::_reversed<T>& r) -> decltype(r.t.rbegin()) { return r.t.rbegin(); }
    template <class T> auto end(details::_reversed<T>& r) -> decltype(r.t.rend()) { return r.t.rend(); }

    template <class T> auto begin(details::_creversed<T> const& cr) -> decltype(cr.t.rbegin()) { return cr.t.rbegin(); }
    template <class T> auto end(details::_creversed<T> const& cr) -> decltype(cr.t.rend()) { return cr.t.rend(); }
}

and then you are able to do the below

for (auto x: reversed(c)) ...
like image 10
bobah Avatar answered Oct 21 '22 00:10

bobah