fork download
  1. #include <iostream>
  2. #include <utility>
  3. #include <tuple>
  4. #include <iterator>
  5. #include <vector>
  6.  
  7. template<class It>
  8. struct range_t {
  9. It b; It e;
  10.  
  11. It begin() const { return b; }
  12. It end() const { return e; }
  13. };
  14.  
  15. template<class It>
  16. range_t<It> range(It s, It f) {
  17. return {s,f};
  18. }
  19. template<class C>
  20. auto backwards(C&& c) {
  21. using std::rbegin; using std::rend;
  22. return range( rbegin(c), rend(c) );
  23. }
  24.  
  25. int main() {
  26. std::cout << "Enter some text (blank line to finish):\n";
  27. std::string line;
  28. std::vector<std::string> lines;
  29. while (std::getline(std::cin, line))
  30. lines.push_back(line);
  31. for (auto&& line:backwards(lines))
  32. std::cout << line << "\n";
  33. }
Success #stdin #stdout 0s 15240KB
stdin
hello world
this is line 2
and this is line 3
the end of the input
stdout
Enter some text (blank line to finish):
the end of the input
and this is line 3
this is line 2
hello world