std::find_if_not的使用
来源:互联网 发布:gis数据挖掘前景 编辑:程序博客网 时间:2024/06/05 15:45
C++11
- // <algorithm>
- template <class InputIterator,
- class UnaryPredicate>
- InputIterator find_if_not (
- InputIterator first,
- InputIterator last,
- UnaryPredicate pred);
返回第一个值不满足给定条件的元素。
该函数等价于:
- template<class _InIt,
- class _Pr> inline
- _InIt _Find_if_not(_InIt _First, _InIt _Last, _Pr _Pred)
- { // find first element that satisfies !_Pred
- for (; _First != _Last; ++_First)
- if (!_Pred(*_First))
- break;
- return (_First);
- }
参数及返回值
- ClassFoo 编辑于 09-26
- 版本0
- 评论0
- 我来编辑
first,last
分别指向一个序列中初始及末尾位置的输入迭代器。这个范围即 [first,last) ,包括 first 到 last 间的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
pred
一元谓词(Unary)函数,以范围中的一个元素为参数,然后返回一个可转换成
bool
类型的值。其返回值表明指定元素是否满足当前函数所检测的条件。
该函数不能修改其参数。
可以是函数指针(Function pointer)类型或函数对象(Function object)类型。
返回
返回指向范围中使谓词函数返回
false
的第一个元素的迭代器。如果 pred 对所有元素返回
true
,则返回 last 迭代器。代码示例
- ClassFoo 编辑于 09-26
- 版本0
- 评论0
- 我来编辑
例 1
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <vector>
- // 用在此处是为了方便简洁, 在实际编程中慎用
- using namespace std;
- namespace ClassFoo{
- bool LessThan5(int & n) { return n < 5; }
- void FindIfNot_1() {
- int iarray[] = { 0, 1, 2, 3, 4, 5, 6, 4, 6, 7, 8 };
- vector<int> foo1(iarray, iarray + sizeof(iarray) / sizeof(int));
- //找出foo1中元素值不小于 5 的第一个元素,并打印其值
- cout << *find_if_not(foo1.begin(), foo1.end(), LessThan5) << endl;
- }
- }
- int main()
- {
- ClassFoo::FindIfNot_1();
- return 0;
- }
5
例 2C++11
- #include <iostream>
- #include <algorithm>
- #include <vector>
- #include <iterator>
- namespace ClassFoo{
- void FindIfNot_2() {
- std::vector<int> v{ 0, 1, 2, 4, 7 };
- // 查找值不小于 5 元素
- auto it = std::find_if_not(std::begin(v), std::end(v), [](int &n) { return n < 5; });
- if (it != std::end(v)) {
- std::cout << "I find it: " << *it << '\n';
- }
- }
- }
- int main()
- {
- ClassFoo::FindIfNot_2();
- return 0;
- }
I find it: 7
扩展信息
- ClassFoo 编辑于 09-26
- 版本0
- 评论0
- 我来编辑
复杂度
O(n),n 为 last - first,对所有元素调用 pred,直到发现不匹配。
数据争用相关
访问在范围 [first,last) 中的部份(或所有)元素,且每个元素确定只被访问一次。
异常安全性相关
如果 pred 或操作某个迭代器抛异常,该函数才会抛异常。
注意 无效参数将导致未定义行为(Undefined behavior)。
0 0
- std::find_if_not的使用
- std::find_if_not
- C++ std::find, std::find_if, std::find_if_not
- std::find, std::find_if, std::find_if_not
- std::vector,std::deque,std::list的区别的使用
- std::map的使用
- std::list的使用
- std::map的使用
- std::hash_map的使用
- std::stable_partition的使用
- std::pair的使用
- std::map的使用
- std::bind的使用
- std::function的使用
- std::bind()的使用
- std::list 的使用
- std::multiset 的使用
- std::bind,std::cref的简单使用
- 赛码网常见接受参数的方法
- Docker基础:文件系统之AUFS
- linux sed、awk、diff命令常用操作
- android 特效闹钟GoogleClock
- react生命周期方法介绍
- std::find_if_not的使用
- 简单抽奖活动js代码
- jquery的ajax方法的回调函数详解
- 在kvm中使用网桥模式(BRIDGE NETWORKING MODE)
- JAVA多线程 基础知识 001
- iOS beginUpdates && endUpdates用法
- android 每行读取本地文件并转换为map对象
- oraclel数据库中的SQL语句使用学习
- 蓝鸥零基础学习HTML5第七讲 常见标签及标签类型的转换