Find conditions about pair vectors

Suppose I have a pair of std :: vector. How can I use the std :: find method effectively to see if at least one element of the vector is equal (false, false)?

thank

+3
source share
1 answer

std::pairoverloads operator==, so you can use std::findfor affirmative:

bool b = std::find(v.begin(), v.end(), std::make_pair(false, false)) == v.end();

and you can use std::find_iffor 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 can be written much more purely 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();
+4
source

Source: https://habr.com/ru/post/1779537/


All Articles