STL reverse in C++

Level 4198 keystrokes

Uses std::reverse to reverse a vector in place and then a string, printing "5 4 3 2 1" and "olleH".

C++ code

// Creates a vector with 1 to 5 in ascending order
1vector<int> nums = {1, 2, 3, 4, 5};
// Reverses the range [begin, end) in place, so nums becomes 5 4 3 2 1
2reverse(nums.begin(), nums.end());
// Prints each element followed by a space: 5 4 3 2 1
3for (int n : nums) cout << n << " ";
// Ends the first line
4cout << endl;
5
6string str = "Hello";
// A string also has iterators, so reverse turns "Hello" into "olleH"
7reverse(str.begin(), str.end());
// Prints olleH
8cout << str << endl;

Output

5 4 3 2 1 
olleH
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C++ code and how often they appear.

  • <× 11
  • ;× 7
  • (× 7
  • )× 7
  • ,× 6
  • .× 4
  • "× 4
  • =× 2
  • :× 1
  • {× 1

More C++ examples at level 4

← C++ typing practice