Skip to content

Instantly share code, notes, and snippets.

@demid5111
Created September 10, 2017 15:51
Show Gist options
  • Save demid5111/d6adf233c32f51276da141e69cbd5f09 to your computer and use it in GitHub Desktop.
Save demid5111/d6adf233c32f51276da141e69cbd5f09 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
int main () {
std::vector<int> counters = {10, 20, 30};
// need mutable as lambda by default is a const expression
auto passByValueLambda = [=] () mutable {
std::cout << "Hello, lambda by value" << std::endl;
// modifying the copy - original vector is not influenced
counters.push_back(1000);
};
passByValueLambda();
std::for_each(counters.begin(), counters.end(),
[](int a) { std::cout << a << std::endl;});
std::cout << "-----------------------------" << std::endl;
// reinitialize the vector gain
counters = {10, 20, 30};
auto passByReferenceLambda = [&] () {
std::cout << "Hello, lambda by reference" << std::endl;
counters.push_back(1000);
};
passByReferenceLambda();
std::for_each(counters.begin(), counters.end(),
[](int a) { std::cout << a << std::endl;});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment