Search⌘ K

- Solution

Explore how C++14 enables lambda functions to initialize variables locally and capture them by reference or value. This lesson helps you understand managing lambda state, focusing on variable sum and its scope inside a lambda within class design.

We'll cover the following...

Solution #

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main(){
std::cout << std::endl;
std::vector<int> intVec = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
int sum{0};
std::for_each(intVec.begin(), intVec.end(), [&sum](int x){ sum += x;});
std::cout << "sum: " << sum << std::endl;
std::cout << std::endl;
}

Explanation

...