December 2018
Intermediate to advanced
702 pages
20h 9m
English
Write generic lambdas:
The following example shows a generic lambda used with the std::accumulate() algorithm first with a vector of integers and then with a vector of strings.
auto numbers = std::vector<int>{0, 2, -3, 5, -1, 6, 8, -4, 9}; auto texts = std::vector<std::string>{"hello"s, " "s, "world"s, "!"s}; auto lsum = [](auto const s, auto const n) {return s + n;}; auto sum = std::accumulate( std::begin(numbers), std::end(numbers), 0, lsum); // sum = 22 auto text = std::accumulate( std::begin(texts), std::end(texts), ""s, lsum); // sum = "hello ...Read now
Unlock full access