Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 查找关于向量对的条件_C++_Vector_Find_Std Pair - Fatal编程技术网

C++ 查找关于向量对的条件

C++ 查找关于向量对的条件,c++,vector,find,std-pair,C++,Vector,Find,Std Pair,假设我有一对std::向量。如何有效地使用std::find方法来查看向量的至少一个元素是否不等于(false,false) 感谢std::pair重载运算符==,因此您可以使用std::find获得肯定的结果: bool b = std::find(v.begin(), v.end(), std::make_pair(false, false)) == v.end(); 对于负片,您可以使用std::find_if: bool b = std::find_if(v.begin(), v.en

假设我有一对std::向量。如何有效地使用std::find方法来查看向量的至少一个元素是否不等于(false,false)


感谢
std::pair
重载
运算符==
,因此您可以使用
std::find
获得肯定的结果:

bool b = std::find(v.begin(), v.end(), std::make_pair(false, false)) == v.end();
对于负片,您可以使用
std::find_if

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();
bool b=std::find_if(v.begin(),v.end(),
std::bind2nd(std::不等于(),
std::make_pair(false,false)))
!= v、 end();
第二个可以用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();
bool b=std::find_if(v.begin(),v.end(),
[](常数std::对p){
返回p!=std::make_pair(false,false);
}) != v、 end();

很抱歉,我想说的是我需要检查所有元素是否都是(false,false)@Banana:您可以使用
find_if
方法并否定结果(如果没有元素使元素等于(false,false),那么每个元素都必须等于(false,false))。另外,C++0x添加了
所有
任何
算法(或类似的算法)。您的第二个解决方案解决了我的问题,但可能是您的第三个解决方案有错误。大约10个错误,我认为stackoverflow不允许我超过注释长度。在stl_algo.h中