fork(1) download
  1. #include <iostream>
  2. #include <utility>
  3. #include <vector>
  4.  
  5. template <typename Container>
  6. class reverse_adaptor
  7. {
  8. public: // Construction
  9. reverse_adaptor(Container &container) :
  10. m_container(container)
  11. {}
  12.  
  13. public: // STL container static polymorphism
  14. auto begin() const -> decltype(std::declval<Container&>().rbegin())
  15. {
  16. return m_container.rbegin();
  17. }
  18.  
  19. auto end() const -> decltype(std::declval<Container&>().rend())
  20. {
  21. return m_container.rend();
  22. }
  23.  
  24. private: // Members
  25. Container &m_container;
  26. };
  27.  
  28. template <typename Container>
  29. reverse_adaptor<Container> make_reverse_adaptor(Container &container)
  30. {
  31. return reverse_adaptor<Container>(container);
  32. }
  33.  
  34. int main()
  35. {
  36. std::vector<int> test = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  37. for (const int i : make_reverse_adaptor(test))
  38. {
  39. std::cout << "i = " << i << std::endl;
  40. }
  41. return 0;
  42. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
i = 10
i = 9
i = 8
i = 7
i = 6
i = 5
i = 4
i = 3
i = 2
i = 1