Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the lambda expression of Clippers 11

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report