Accumulate Algorithm in C++
Level 5267 keystrokes
accumulate folds a range into one value: with the start value 0 it adds 1 to 10 (55), and with the start value 1 and a multiplying lambda it computes 10! (3628800).
C++ code
// Numbers 1 to 101vector<int> nums = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};2 // Without an operation, accumulate adds the elements to the start value 03int sum = accumulate(nums.begin(), nums.end(), 0);// Prints "Sum: 55"4cout << "Sum: " << sum << endl;5 // Start value 1 (0 would make the product 0) and a custom operation6int product = accumulate(nums.begin(), nums.end(), 1, [](int a, int b) { // a is the running result, b the next element7 return a * b;8});// Prints "Product: 3628800" (10!)9cout << "Product: " << product << endl;Output
Sum: 55 Product: 3628800
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.
,× 15<× 13(× 7)× 7;× 6.× 4"× 4=× 3:× 2{× 2