STL for_each in C++
Level 4178 keystrokes
Calls for_each twice: first with a lambda taking int& that doubles each element in place, then with one that prints the elements, 2 4 6 8 10.
C++ code
1vector<int> nums = {1, 2, 3, 4, 5};// Apply the lambda below to every element2for_each(nums.begin(), nums.end(), // int& lets the lambda change the element itself: each value is doubled3 [](int& n) { n *= 2; });// Second pass over the now doubled vector4for_each(nums.begin(), nums.end(), // Takes a copy and prints it: 2 4 6 8 105 [](int n) { cout << n << " "; });6cout << endl;Output
2 4 6 8 10
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)× 8<× 7;× 6.× 4{× 3}× 3_× 2"× 2