How to use the lambda expression of Clippers 11
This article mainly explains "how to use the lambda expression of Category 11". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use the lambda expression of Category 11".
Variable lambda
Suppose you have the following vector, and the content saved is the student's exam results:
Vector score {45, 70, 56, 86, 28, 60, 90}
You can use the following code to find the first pass:
Find_if (score.begin (), score.end (), [] (int v) {return (v > = 60);})
If you need to find the nth pass, it's natural to consider using the following code:
Int counter = 2 find if (score.begin (), score.end (), [counter] (int v) {return (v > = 60) & & (--counter = = 0);})
Unfortunately, there will be a compilation error that tells you that counter is read-only. The reason for this is that there are few scenarios in the lambda expression that need to modify the capture value, so the default capture value has the const attribute. If this happens in this case, you do want to modify the capture value, and clocking 11 uses the mutable keyword to solve this problem. Take a look at the complete code:
Int counter = 2
Auto iter find_if (score.begin (), score.end ())
[counter] (int v) mutable {
Return (v > = 60) & & (--counter = = 0)
});
Cout