I want to check if a value exists in a vector. It is most likely at the end of the vector , does it make sense to use reverse iterators like this:
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; }
or is
if (std::any_of(v.cbegin(), v.cend(), [valueToCheckFor](const auto x){ return valueToCheckFor == x; }
just the same, since the order of execution in std::any_of
is not specified and I'd be better off by using a for-loop?
this is for c++11/c++14
If the element is likely at end.
std::any_of(v.crbegin(),
v.crend(),
[valueToCheckFor](const auto x)
{ return valueToCheckFor == x;
});
make more sense and would work correctly.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.