是否可以使用单个 std::all_of()
调用并同时使用多个元素/条件 or'd?或者这是否违反了功能?
例子:
if(std::all_of(vector.begin(), vector.end(), 0||1||2||3) == true)
{
//do something
}
谢谢,
最佳答案
你必须使用一个谓词,例如
vector<t> v;
if(std::all_of(v.begin(), v.end(), [](const t& el){
return el == 0 || el == 1 || el == 2 || el == 3;
};)
{
//do something
}
获得你想要的行为。
来自 cppreference.com你明白了
template< class InputIt, class UnaryPredicate >
bool all_of( InputIt first, InputIt last, UnaryPredicate p );
在哪里
p
- unary predicate . The signature of the predicate function should be equivalent to the following:
bool pred(const Type &a);
The signature does not need to have
const &
, but the function must not modify the objects passed to it. The typeType
must be such that an object of typeInputIt
can be dereferenced and then implicitly converted toType
.
对您来说最重要的部分是 pred
的签名
bool pred(const Type &a);
这意味着您用作 pred
的仿函数/lambda/方法应该采用 Type
类型的参数并返回 bool
。
关于c++ - std::all_of() 的多个 UnaryPredicates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35399001/