Created
September 10, 2017 15:51
-
-
Save demid5111/d6adf233c32f51276da141e69cbd5f09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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