std::find_if , std::find_if_not in C++

std :: find_if

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.
Function Template :

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

first, last :range which contains all the elements between first
and last, including the element pointed by first but
not the element pointed by last.

pred : Unary function that accepts an element in the range
as argument and returns a value in boolean.

Return value :
Returns an iterator to the first element in the range
[first, last] for which pred(function) returns true. If
no such element is found, the function returns last.

std :: find_if_not

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.
Function Template :

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

Return value :
Returns an iterator to the first element in the range
[first, last] for which pred(function) returns false.
filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to illustrate
// std::find_if and std::find_if_not
#include <bits/stdc++.h>
  
// Returns true if argument is odd
bool IsOdd(int i)
{
    return i % 2;
}
  
// Driver code
int main()
{
    std::vector<int> vec{ 10, 25, 40, 55 };
  
    // Iterator to store the position of element found
    std::vector<int>::iterator it;
  
    // std::find_if
    it = std::find_if(vec.begin(), vec.end(), IsOdd);
    std::cout << "The first odd value is " << *it << '\n';
  
        // Iterator to store the position of element found
        std::vector<int>::iterator ite;
  
    // std::find_if_not
    ite = std::find_if_not(vec.begin(), vec.end(), IsOdd);
  
    std::cout << "The first non-odd(or even) value is " << *ite << '\n';
  
        return 0;
}

chevron_right


Output:



The first odd value is 25
The first non-odd(or even) value is 10

Related Articles:

This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.