[英]Is there something like std::any_of that will apply predicate to all elements
[英]Does it make sense to give reverse iterators to std::any_of and the like?
我想检查向量中是否存在一个值。 它很可能在 vector 的末尾,使用这样的反向迭代器是否有意义:
std::vector<int> v{};
//... add a lot of values ...
const int valueToCheckFor{42};
if (std::any_of(v.crbegin(), v.crend(), [valueToCheckFor](const auto x){ return valueToCheckFor == x; }
或者是
if (std::any_of(v.cbegin(), v.cend(), [valueToCheckFor](const auto x){ return valueToCheckFor == x; }
一样,因为没有指定std::any_of
的执行顺序,我最好使用 for 循环?
这是用于 c++11/c++14
如果元素很可能在结尾。
std::any_of(v.crbegin(),
v.crend(),
[valueToCheckFor](const auto x)
{ return valueToCheckFor == x;
});
更有意义并且可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.