In this article we will discuss how to use STL Algorithm std::any_of() with both lambda function and function pointer.

std::any_of is an STL algorithm introduced in c++11.

Need of std::any_of()

This STL algorithm is useful when you have a range of element and you want to check if any of the given elements in range satisfies a given condition.

std::any_of() Usage Deatils

std::any_of() accepts a range of elements and a Unary Predicate (callback)  as an argument.

std::any_of() iterates over the given range and for each elements calls the given callback i.e. Unary Predicate. If for any element then given predicate returns true then it stops the further iteration and return true, else it returns false.

std::any_of() Examples

How to use std::any_of() with two different type of callbacks i.e Lambda Function and Function pointer.

Using std::any_of() with Lambda Function

Suppose We have a vector of strings i.e.

Now we want to check if this vector contains any string with size 4. Let’s do this using std::any_of() i.e.

std::any_of() will iterate through all the strings in vector and for each string in vector it calls the passed lambda function, that checks if size of string is 4. If for any string the lambda function returns true, std::any_of() will stop the further iteration and returns true else it returns false.

Now check if the above vector contains any string that starts with char ‘P’ i.e.

Using std::any_of() with a Function Pointer

Suppose we have a string i.e.

Now let’s check if given string contains all lower case letters i.e. not a single upper case char i.e.

Checkout complete example as follows,


To compile the above code use following command in linux,

g++ –std=c++11 example.cpp


Click Here to Subscribe for more Articles / Tutorials like this.