STL fill in C++

Level 4199 keystrokes

Sets every element of a vector to 7 with fill, then sets only the first three to 0, printing 7 7 7 7 7 and 0 0 0 7 7.

C++ code

// A vector of five ints, all initialized to 0
1vector<int> nums(5);
// Assign 7 to every element in the range
2fill(nums.begin(), nums.end(), 7);
// Prints 7 7 7 7 7
3for (int n : nums) cout << n << " ";
4cout << endl;
5
// Overwrite only the first three elements with 0
6fill(nums.begin(), nums.begin() + 3, 0);
// Prints 0 0 0 7 7
7for (int n : nums) cout << n << " ";
8cout << endl;

Output

7 7 7 7 7 
0 0 0 7 7 
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.

  • <× 13
  • (× 9
  • )× 9
  • ;× 7
  • ,× 4
  • .× 4
  • "× 4
  • :× 2
  • +× 1
  • >× 1

More C++ examples at level 4

← C++ typing practice