More Fixes

This commit is contained in:
bk2dcradle 2016-01-08 01:21:38 +05:30
parent 19ead59c1f
commit e6866f5a26

View File

@ -840,16 +840,22 @@ tester.push_back(make_pair(5, 0));
// sort is from the <algorithm> header // sort is from the <algorithm> header
sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) { sort(tester.begin(), tester.end(), [](const pair<int, int>& lhs, const pair<int, int>& rhs) {
return lhs.second < rhs.second; return lhs.second < rhs.second;
}); });
// 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);
} }
@ -861,13 +867,10 @@ int weight[3] = {30, 50, 10};
// Here's where lambda expressions come in handy // Here's where lambda expressions come in handy
sort(dog_ids.begin(), dog_ids.end(), [&weight](const int &lhs, const int &rhs) { 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)