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.

bool any_of (InputIterator start, InputIterator end, UnaryPredicate callback);

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.

std::vector<std::string> vecOfStrs =
	{ "Hi", "Hello", "test", "first", "second", "third", "fourth" };

Now we want to check if this vector contains any string with size 4. Let’s do this using std::any_of() i.e.
/*
Check if vector contains any string with size 4.
*/
bool result = std::any_of(vecOfStrs.begin(), vecOfStrs.end(), [](const std::string & str){
														return str.size() == 4;
														});

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.

/*
 Check if vector contains any string that starts with char 'P'
 */
result = std::any_of(vecOfStrs.begin(), vecOfStrs.end(), [](const std::string & str) {
														return str.size() > 0 && str[0] == 'P';
													});

Using std::any_of() with a Function Pointer

Suppose we have a string i.e.

std::string str = "testString";

Now let’s check if given string contains all lower case letters i.e. not a single upper case char i.e.
/*
 Check if given string contains all lower case letters i.e. not a single upper case char
 */
result = std::any_of(str.begin(), str.end(), ::isupper);

Checkout complete example as follows,
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

int main()
{
	std::vector<std::string> vecOfStrs =
	{ "Hi", "Hello", "test", "first", "second", "third", "fourth" };

	/*
	 Check if vector contains any string with size 4.
	 */
	bool result = std::any_of(vecOfStrs.begin(), vecOfStrs.end(), [](const std::string & str){
																					return str.size() == 4;
																					});

	std::cout << "vector contains any string with size 4 | Result = " << result << std::endl;

	/*
	 Check if vector contains any string that starts with char 'P'
	 */
	result = std::any_of(vecOfStrs.begin(), vecOfStrs.end(), [](const std::string & str) {
															return str.size() > 0 && str[0] == 'P';
														});

	std::cout<< "vector contains any string that starts with char 'P' | Result = " 	<< result << std::endl;

	std::string str = "testString";

	/*
	 Check if given string contains all lower case letters i.e. not a single upper case char
	 */

	result = std::any_of(str.begin(), str.end(), ::isupper);

	std::cout << "str  = " << str << std::endl;
	std::cout<< "Check if given string contains all lower case letters | Result = " << result << std::endl;
	return 0;
}


Output:
vector contains any string with size 4 | Result = 1
vector contains any string that starts with char 'P' | Result = 0
str  = testString
Check if given string contains all lower case letters | Result = 1

To compile the above code use following command in linux,

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