开发者

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();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜