mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-07 07:16:42 +02:00
More Fixes
This commit is contained in:
@@ -844,12 +844,18 @@ sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int,
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Notice the syntax of the lambda expression,
|
// Notice the syntax of the lambda expression,
|
||||||
// [] in the lambda is used to "capture" variables.
|
// [] in the lambda is used to "capture" variables
|
||||||
// For Example:
|
// The "Capture List" defines what from the outside of the lambda should be available inside the function body and how.
|
||||||
|
// It can be either:
|
||||||
|
// 1. a value : [x]
|
||||||
|
// 2. a reference : [&x]
|
||||||
|
// 3. any variable currently in scope by reference [&]
|
||||||
|
// 4. same as 3, but by value [=]
|
||||||
|
// Example:
|
||||||
|
|
||||||
vector<int> dog_ids;
|
vector<int> dog_ids;
|
||||||
// number_of_dogs = 3;
|
// number_of_dogs = 3;
|
||||||
for(int i = 0; i < 3; i++){
|
for(int i = 0; i < 3; i++) {
|
||||||
dog_ids.push_back(i);
|
dog_ids.push_back(i);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -864,10 +870,7 @@ sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) {
|
|||||||
return weight[lhs] < weight[rhs];
|
return weight[lhs] < weight[rhs];
|
||||||
});
|
});
|
||||||
// Note we captured "weight" by reference in the above example.
|
// Note we captured "weight" by reference in the above example.
|
||||||
|
// More on Lambdas in C++ : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11
|
||||||
// lambda are really useful for the case of structs
|
|
||||||
// You can use lambda expressions instead of overloading
|
|
||||||
// the "<" operator
|
|
||||||
|
|
||||||
///////////////////////////////
|
///////////////////////////////
|
||||||
// Range For (C++11 and above)
|
// Range For (C++11 and above)
|
||||||
|
Reference in New Issue
Block a user