diff options
author | bk2dcradle <ankitsultana@gmail.com> | 2016-01-08 01:21:38 +0530 |
---|---|---|
committer | bk2dcradle <ankitsultana@gmail.com> | 2016-01-08 01:21:38 +0530 |
commit | e6866f5a26dab28d2d1b5628fbb18139c36a5139 (patch) | |
tree | 1a48c85ee311b54a4c1cd08c49e2652b09e386e5 /c++.html.markdown | |
parent | 19ead59c1fde3623bc29e1fe56f33f2587c97d3a (diff) |
More Fixes
Diffstat (limited to 'c++.html.markdown')
-rw-r--r-- | c++.html.markdown | 25 |
1 files changed, 14 insertions, 11 deletions
diff --git a/c++.html.markdown b/c++.html.markdown index 31dbe064..44cad665 100644 --- a/c++.html.markdown +++ b/c++.html.markdown @@ -840,16 +840,22 @@ tester.push_back(make_pair(5, 0)); // sort is from the <algorithm> header 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, -// [] in the lambda is used to "capture" variables. -// For Example: +// [] in the lambda is used to "capture" variables +// 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; // number_of_dogs = 3; -for(int i = 0; i < 3; i++){ +for(int i = 0; i < 3; i++) { dog_ids.push_back(i); } @@ -861,13 +867,10 @@ int weight[3] = {30, 50, 10}; // Here's where lambda expressions come in handy 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. - -// lambda are really useful for the case of structs -// You can use lambda expressions instead of overloading -// the "<" operator +// More on Lambdas in C++ : http://stackoverflow.com/questions/7627098/what-is-a-lambda-expression-in-c11 /////////////////////////////// // Range For (C++11 and above) |