How to use lambda expression in std::find_if (C++)

Lambda function / expression is a nice feature of C++11. It enables a programmer — which means you — to define and use a function inline; that is, you don’t need to define your function in outer scope.

This feature greatly reduces the need to redundantly define functions; you can define a function in a place where you want to, in the same scope!

In this post, I will tell you how you can use its amazing power to find elements in an STL container (std::vector, std::list, etc.) according to some rules which would be set through lambda functions. Below is the example of “IsOdd” logic, which is originally found here:

Here, you can see that I’ve used a normal function to define a logic or criteria for finding elements in a container. We will now use lambda expression instead:

Both of the above programs do the same job, however, in the second program, we didn’t have to define a function of such a simple logic outside the main scope, thus shortening our code.


In this tutorial, I have showed you the ways to use lambda expression with std::find_if function for manipulating STL containers.
Do you have any questions regarding this tutorial? Tell me in the comments section! Also like our page on Facebook.