1.Lambdas with captures
AdvancedGiven std::vector<int> nums = {4, 11, 7, 20, 15, 2}; and int limit = 10;, use std::count_if with a lambda that captures limit to count the numbers above it. Then use std::for_each with a lambda that captures a total by reference to add up all the numbers.
Show hint
Capture by value with [limit] and by reference with [&total].
Show solution
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> nums = {4, 11, 7, 20, 15, 2};
int limit = 10;
auto above = std::count_if(nums.begin(), nums.end(), [limit](int n) { return n > limit; });
int total = 0;
std::for_each(nums.begin(), nums.end(), [&total](int n) { total += n; });
std::cout << above << ' ' << total << '\n';
}3 59