Reverse iterator in C++

Level 3119 keystrokes

rbegin() points to the last element and rend() to the position before the first. ++ on a reverse iterator moves backwards, so the vector is printed as 5 4 3 2 1.

C++ code

1vector<int> v = {1, 2, 3, 4, 5};
// Starts at the last element; ++it moves toward the front until rend()
2for (auto it = v.rbegin(); it != v.rend(); ++it) {
// *it reads the current element: 5, 4, 3, 2, 1
3 cout << *it << " ";
4}
// Ends the line after the loop
5cout << endl;

Output

5 4 3 2 1 
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.

  • <× 7
  • ;× 5
  • ,× 4
  • (× 3
  • )× 3
  • =× 3
  • .× 2
  • "× 2
  • {× 2
  • }× 2

More C++ examples at level 3

← C++ typing practice