find conditions about vectors of pairs
suppose I have a s开发者_高级运维td::vector of pair. How can I use, efficiently, method std::find to see whether at least one element of the vector is not equal to (false, false)?
Thanks
std::pair
overloads operator==
, so you can use std::find
for the affirmative:
bool b = std::find(v.begin(), v.end(), std::make_pair(false, false)) == v.end();
and you can use std::find_if
for the negative:
bool b = std::find_if(v.begin(), v.end(),
std::bind2nd(std::not_equal_to<std::pair<bool, bool> >(),
std::make_pair(false, false)))
!= v.end();
The second one can be written much more cleanly in C++0x:
bool b = std::find_if(v.begin(), v.end(),
[](const std::pair<bool, bool> p) {
return p != std::make_pair(false, false);
}) != v.end();
精彩评论