fork download
  1. #include <algorithm> // For std::reverse
  2. #include <iostream> // For console output
  3. #include <vector> // For std::vector
  4. using namespace std;
  5.  
  6. // Used to print elements of a sequence (like vector).
  7. template <typename Sequence>
  8. void print(const Sequence& s) {
  9. cout << "[ ";
  10. for (const auto& x : s) {
  11. cout << x << ' ';
  12. }
  13. cout << "]" << endl;
  14. }
  15.  
  16. int main() {
  17. cout << "Original vector:\n";
  18. vector<int> v{10, 20, 30, 40, 50};
  19. print(v);
  20.  
  21. vector<int> v2(v.rbegin(), v.rend());
  22. cout << "\nReversed copy using reverse iterators:\n";
  23. print(v2);
  24.  
  25. reverse(v.begin(), v.end());
  26. cout << "\nReversed in-place using std::reverse():\n";
  27. print(v);
  28. }
  29.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
Original vector:
[ 10 20 30 40 50 ]

Reversed copy using reverse iterators:
[ 50 40 30 20 10 ]

Reversed in-place using std::reverse():
[ 50 40 30 20 10 ]