加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – any_of Versus find_if

发布时间:2020-12-16 03:17:03 所属栏目:百科 来源:网络整理
导读:C 11引入了any_of算法. 这似乎像find_if完全一样. 说我有一个函子:function bool(int) FOO; 和一个数组:vector int酒吧; 看来这两个电话是完全一样的: any_of(bar.begin(),bar.end(),foo); 和 bar.end() != find_if(bar.begin(),foo); 我进一步感觉,all_o
C 11引入了any_of算法.

这似乎像find_if完全一样.

说我有一个函子:function< bool(int)> FOO;
和一个数组:vector< int>酒吧;

看来这两个电话是完全一样的:

any_of(bar.begin(),bar.end(),foo);

bar.end() != find_if(bar.begin(),foo);

我进一步感觉,all_of和none_of可以通过否定find_if语句来实现.

这些算法正是在这里做比较才能为我们结束,还是有一个有用的我不明白?

解决方法

我相信你是对的,他们只是更方便的功能接口,可以通过其他方式实现.

将它们添加到标准(N2666)中的建议说:

These three algorithms provide the ordinary mathematical operations ?,?,and ?: given a
range and a predicate,determine whether that predicate is true for all elements; whether
there exists an element for which the predicate is true; or whether there exist no elements
for which the predicate is true. Strictly speaking there is no need to provide all three of
these algorithms (!none_of and any_of are equivalent),but all three of these operations
feel equally fundamental.

这些名称比包含find_if和(in)等式的表达更自然,更容易阅读(肯定是非专家C程序员).

GCC的标准库通过简单地调用其他函数实现它们:

all_of(first,last,pred)是return last == std :: find_if_not(first,pred);

none_of(first,pred)是return last == std :: find_if(first,pred);

any_of(first,pred)是return!none_of(first,pred);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读