Last active
June 22, 2017 13:26
-
-
Save arvidsson/7231973 to your computer and use it in GitHub Desktop.
Reverse iteration in C++11 range-based for loops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// public domain | |
#ifndef INCLUDED_REVERSE_ITERATE_HPP | |
#define INCLUDED_REVERSE_ITERATE_HPP | |
/* | |
Template used for reverse iteration in C++11 range-based for loops. | |
std::vector<int> v = {1, 2, 3, 4, 5}; | |
for (auto x : reverse_iterate(v)) | |
std::cout << x << " "; | |
*/ | |
template <typename T> | |
class reverse_range | |
{ | |
T &x; | |
public: | |
reverse_range(T &x) : x(x) {} | |
auto begin() const -> decltype(this->x.rbegin()) | |
{ | |
return x.rbegin(); | |
} | |
auto end() const -> decltype(this->x.rend()) | |
{ | |
return x.rend(); | |
} | |
}; | |
template <typename T> | |
reverse_range<T> reverse_iterate(T &x) | |
{ | |
return reverse_range<T>(x); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment