Open utterances-bot opened 2 years ago
Hi hackingcpp's guys, thanks for your awesome work to demonstrate STL's algorithm.
It seems that the example code of iota
is unable to run seamlessly in Compiler Explorer. The link of example code, and the compile error looks like:
<source>: In function 'int main()':
<source>:19:73: error: expected '}' at end of input
19 | auto const product = reduce(begin(w), end(w), 1.0, std::multiplies<>{});
| ^
<source>:5:13: note: to match this '{'
5 | int main () {
| ^
<source>:19:12: warning: unused variable 'product' [-Wunused-variable]
19 | auto const product = reduce(begin(w), end(w), 1.0, std::multiplies<>{});
| ^~~~~~~
I think you could fix this problem by adding a missing curly brace
#include <vector>
#include <iostream>
#include <numeric>
int main () {
std::vector<int> v;
v.resize(9,0);
// fill subrange (as shown in image)
iota(begin(v)+2, begin(v)+7, 1);
for (int x : v) { std::cout << x << ' '; } // 0 0 1 2 3 4 5 0 0
std::cout << '\n';
// fill entire vector
iota(begin(v), end(v), 3);
for (int x : v) { std::cout << x << ' '; } // 3 4 5 6 7 8 9 10 11
auto const s47 = reduce(begin(v), end(v), 47); //47+1+9+7+3+2+8 = 77
std::cout << "s47: " << s47 << '\n';
std::vector<double> w {2.0, 1.5, 3.0, 1.5};
auto const product = reduce(begin(w), end(w), 1.0, std::multiplies<>{});
// 1.0 * 2.0 * 1.5 * 3.0 * 1.5 = 13.5
} // => the missing curly brace
C++ Standard Library Numeric Operations | hacking C++
Introduction, examples and visual explanations of numeric standard algorithms operating on (sub-)ranges.
https://hackingcpp.com/cpp/std/algorithms/numeric.html