具有多个谓词的C ++ 11算法 [英] C++11 algorithms with multiple predicates
本文介绍了具有多个谓词的C ++ 11算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
算法标题中的
std :: find_if
等函数非常有用,是每个调用 count_if
只能使用1个谓词的事实。
Functions such as std::find_if
from the algorithm
header are really useful, but 1 serious limit for me is the fact that I can only use 1 predicate for each call to count_if
.
例如给定一个容器 std :: vector
我想同时应用 find_if
的同一个迭代谓词在标准库中有一些可以保持这种功能的方法?
For example given a container like an std::vector
I would like to apply, at the same time, with the same iteration of find_if
, multiple predicates; there is something in the standard library that makes this possible while keeping this functional approach ?
推荐答案
/ p>
Just combine them with a lambda:
std::find_if(begin(vec), end(vec),
[](elem_t val) {
return f1(val) || f2(val);
});
这篇关于具有多个谓词的C ++ 11算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文