STL transform in C++
Level 4192 keystrokes
Uses transform with a lambda to write the square of each element into a second vector, then prints 1 4 9 16 25.
C++ code
1vector<int> nums = {1, 2, 3, 4, 5};// Destination vector with the same size, filled with zeros2vector<int> squared(nums.size());// transform reads nums and writes each result into squared, starting at its beginning3transform(nums.begin(), nums.end(), squared.begin(), // The lambda applied to each element returns its square4 [](int n) { return n * n; });// Print 1 4 9 16 255for (int n : squared) cout << n << " ";Output
1 4 9 16 25
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.
(× 8)× 8,× 7<× 6;× 5.× 4"× 2{× 2}× 2>× 2