📜  C++中的std :: find_if,std :: find_if_not

📅  最后修改于: 2021-05-30 07:10:39             🧑  作者: Mango

std :: find_if

返回一个迭代器,该迭代器返回pred(Unary 函数)返回true的[first,last]范围内的第一个元素。如果找不到这样的元素,则该函数最后返回。
函数模板:

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

返回一个迭代器,该迭代器返回pred(Unary 函数)返回false的[first,last]范围内的第一个元素。如果找不到这样的元素,则该函数最后返回。
函数模板:

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.
// CPP program to illustrate
// std::find_if and std::find_if_not
#include 
  
// Returns true if argument is odd
bool IsOdd(int i)
{
    return i % 2;
}
  
// Driver code
int main()
{
    std::vector vec{ 10, 25, 40, 55 };
  
    // Iterator to store the position of element found
    std::vector::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::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;
}

输出:

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

相关文章:

  • std :: search
  • std ::查找
  • std :: nth_element
  • std :: find_end
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”