Iterator basics in C++

Level 3114 keystrokes

An iterator points to an element of a container. The loop starts at begin(), advances with ++ until it reaches end(), and reads each element with *, printing 10 20 30.

C++ code

1vector<int> v = {10, 20, 30};
// begin() points to the first element; end() points one past the last, so it is never read
2for (auto it = v.begin(); it != v.end(); ++it) {
// *it reads the element the iterator points to
3 cout << *it << " ";
4}
// Ends the line after 10 20 30
5cout << endl;

Output

10 20 30 
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
  • (× 3
  • )× 3
  • =× 3
  • ,× 2
  • .× 2
  • "× 2
  • {× 2
  • }× 2

More C++ examples at level 3

← C++ typing practice