You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

reverse_iterator.h 770B

123456789101112131415161718192021222324252627282930313233343536373839
  1. // Taken from https://gist.github.com/arvidsson/7231973
  2. #ifndef STARWELS_REVERSE_ITERATOR_H
  3. #define STARWELS_REVERSE_ITERATOR_H
  4. /**
  5. * Template used for reverse iteration in C++11 range-based for loops.
  6. *
  7. * std::vector<int> v = {1, 2, 3, 4, 5};
  8. * for (auto x : reverse_iterate(v))
  9. * std::cout << x << " ";
  10. */
  11. template <typename T>
  12. class reverse_range
  13. {
  14. T &m_x;
  15. public:
  16. reverse_range(T &x) : m_x(x) {}
  17. auto begin() const -> decltype(this->m_x.rbegin())
  18. {
  19. return m_x.rbegin();
  20. }
  21. auto end() const -> decltype(this->m_x.rend())
  22. {
  23. return m_x.rend();
  24. }
  25. };
  26. template <typename T>
  27. reverse_range<T> reverse_iterate(T &x)
  28. {
  29. return reverse_range<T>(x);
  30. }
  31. #endif // STARWELS_REVERSE_ITERATOR_H