initializer_list in C++

Level 4157 keystrokes

A function that takes an initializer_list<int> accepts brace lists of any length; it is called with 3 and then 5 values and prints them.

C++ code

// initializer_list<int> receives a {...} list of ints of any length
1void printList(initializer_list<int> list) {
// It can be iterated with a range-based for loop
2 for (int n : list) {
// Print each value followed by a space
3 cout << n << " ";
4 }
// End the line after the list
5 cout << endl;
6}
7
// Three values: prints 1 2 3
8printList({1, 2, 3});
// Same function with five values: prints 10 20 30 40 50
9printList({10, 20, 30, 40, 50});

Output

1 2 3 
10 20 30 40 50 
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
  • ,× 6
  • ;× 4
  • (× 4
  • )× 4
  • {× 4
  • }× 4
  • "× 2
  • _× 1
  • :× 1

More C++ examples at level 4

← C++ typing practice