std::find_if , std::find_if_not in C++ (original) (raw)

Last Updated : 21 Aug, 2025

std :: find_if and std :: find_if_not are algorithm functions in C++ Standard Library in header file. These functions provide an efficient way to search for an element in a container using a predicate function.

std :: find_if

This function returns an iterator to the first element in the range [first, last) for which pred(Unary Function) returns true. If no such element is found, the function returns last.

Syntax

InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

Parameters

Return Value

Example

The following C++ program illustrates the use of the find_if() function.

C++ `

// CPP program to illustrate std::find_if #include <bits/stdc++.h> using namespace std;

// Returns true if argument is odd bool IsOdd(int i) { return i % 2; }

// Driver code int main() { vector vec{ 10, 25, 40, 55 };

// Iterator to store the position of element found
vector<int>::iterator it;

// std::find_if
it = find_if(vec.begin(), vec.end(), IsOdd);

cout << "The first odd value is " << *it << '\n';

return 0;

}

`

Output

The first odd value is 25

std :: find_if_not

This function returns an iterator to the first element in the range [first, last) for which pred(Unary Function) returns false. If no such element is found, the function returns last.

Syntax

InputIterator find_if_not (InputIterator first, InputIterator last, UnaryPredicate pred);

Parameters

Return value

Example

The following C++ program illustrates the use of the find_if_not() function.

C++ `

// CPP program to illustrate std::find_if_not #include <bits/stdc++.h> using namespace std; // Returns true if argument is odd

bool IsOdd(int i) { return i % 2; }

// Driver code int main() { vector vec{ 10, 25, 40, 55 };

// Iterator to store the position of element found
vector<int>::iterator it;

// std::find_if_not
it = find_if_not(vec.begin(), vec.end(), IsOdd);

cout << "The first non-odd(or even) value is " << *it
     << '\n';

return 0;

}

`