Open In App

any_of() Function in C++ STL

Improve
Improve
Like Article
Like
Save
Share
Report

any_of() is the C++ function defined in <algorithm> library in STL. This function determines whether even one element in a given range satisfies a specified criterion. If at least one element meets the property, then it returns true; otherwise, it returns false. Also if the range is empty then this function returns false. any_of() function takes 3 arguments, the first element, the last element, and a function condition_function.

Function Template:

template <class InputIterator, class UnaryPredicate>
bool any_of (InputIterator start_limit, InputIterator end_limit, UnaryPredicate condition_function);

Parameters:

start_limit – It is the first element in the range specified.

end_limit – It is the last element in the range.

condition_function – It is a function which accepts the argument from the range.

Time Complexity: O(n)
Auxiliary Space: O(1)

Example:

C++




// C++ Program to demonstrate
// working of any_of()
#include <algorithm>
#include <iostream>
 
using namespace std;
 
int main()
{
    // Initializing the array
    int ar[6] = { 2, 6, 7, 10, 8, 4 };
 
    // Checking if any odd number is
    // present or not in the array
    if (any_of(ar, ar + 6,
               [](int x) { return x % 2 != 0; })) {
        cout << "There exists odd number in the array";
    }
    else {
        cout << "No odd number found in the array";
    }
 
    return 0;
}


Output

There exists odd number in the array

Last Updated : 24 Aug, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads