Auto with Vector in C++

Level 266 keystrokes

Loops over the vector {1, 2, 3} with a range-based for loop whose variable type is deduced by auto, and prints 1, 2 and 3.

C++ code

// Creates a vector with the values 1, 2 and 3
1vector<int> v = {1, 2, 3};
// auto deduces n as int from the element type of v
// n receives a copy of each element in turn
2for (auto n : v) {
// Prints each element on its own line
3 cout << n << endl;
4}

Output

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

  • <× 5
  • ,× 2
  • ;× 2
  • {× 2
  • }× 2
  • :× 1
  • (× 1
  • )× 1
  • =× 1
  • >× 1

More C++ examples at level 2

← C++ typing practice