STL copy in C++

Level 4248 keystrokes

Copies all elements of one vector into another with copy, then copies only the first three, printing 1 2 3 4 5 and 123.

C++ code

1vector<int> src = {1, 2, 3, 4, 5};
// The destination must already have room: five zeros
2vector<int> dest(5);
// Copy the range [begin, end) of src to the start of dest
3copy(src.begin(), src.end(), dest.begin());
// Prints 1 2 3 4 5
4for (int n : dest) cout << n << " ";
// End the first line
5cout << endl;
6
// Copy only the first three elements (begin() + 3 is the end of the range)
7copy(src.begin(), src.begin() + 3, dest.begin());
// No separators are printed, so the output is 123
8cout << dest[0] << dest[1] << dest[2] << endl;

Output

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

  • <× 16
  • (× 10
  • )× 10
  • ,× 8
  • ;× 7
  • .× 6
  • [× 3
  • ]× 3
  • "× 2
  • >× 2

More C++ examples at level 4

← C++ typing practice