![](/img/trans.png)
[英]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.