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)
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))
...
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
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)) ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With