Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the lambda expression of Clippers 11

Shulou Source: shulou.com Published: 2022-06-01 20:48:41 10月05日 Update

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

Tags: Expressions codes content grades learning situations problems regrets keys keywords reasons scenarios students that is attributes ideas articles more for knowledge Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno macOS Shulou Information Docker Microsoft