algorithm::find_if_not()
描述 (Description)
C ++函数std::algorithm::find_if_not()查找满足条件的元素的最后一次出现。 它使用一元谓词来指定条件。
声明 (Declaration)
以下是std :: algorithm :: find_if_not()函数形式std :: algorithm头的声明。
C++11
template <class InputIterator, class UnaryPredicate>
InputIterator find_if_not(InputIterator first, InputIterator last, UnaryPredicate pred);
参数 (Parameters)
first - 将迭代器输入到初始位置。
last - 最终位置的最终迭代器。
pred - 一个接受一个参数并返回bool的一元谓词。
返回值
将迭代器返回到范围(first,last)中的第一个元素,其中一元谓词返回false。 如果找不到这样的元素,则函数最后返回。
异常 (Exceptions)
如果迭代器上的谓词或操作抛出异常,则抛出异常。
请注意,无效参数会导致未定义的行为。
时间复杂
线性。
例子 (Example)
以下示例显示了std :: algorithm :: find_if_not()函数的用法。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool unary_pred(int n) {
return ((n % 2) == 0);
}
int main(void) {
vector<int> v = {2, 4, 61, 8, 10};
auto it = find_if_not(v.begin(), v.end(), unary_pred);
if (it != end(v))
cout << "First odd number is " << *it << endl;
v = {2, 4, 6, 8, 10};
it = find_if_not(v.begin(), v.end(), unary_pred);
if (it == end(v))
cout << "Only enven elements present in the sequence." << endl;
return 0;
}
让我们编译并运行上面的程序,这将产生以下结果 -
First odd number is 61
Only enven elements present in the sequence.