C++ 如何检查向量的向量是否具有所有相同的值

C++ 如何检查向量的向量是否具有所有相同的值,c++,arrays,if-statement,vector,equals,C++,Arrays,If Statement,Vector,Equals,要检查向量是否具有所有相等的值,我通常会执行以下操作: std::vector<bool> myVec(3); if(std::adjacent_find(myVec.begin(), myVec.end(), std::not_equal_to<>()) == myVec.end()){ // do if every value is the same } std::vector myVec(3); 如果(std::nexting_find(myVec.be

要检查向量是否具有所有相等的值,我通常会执行以下操作:

std::vector<bool> myVec(3);

if(std::adjacent_find(myVec.begin(), myVec.end(), std::not_equal_to<>()) == myVec.end()){
    // do if every value is the same
}
std::vector myVec(3); 如果(std::nexting_find(myVec.begin()、myVec.end()、std::not_equal_to())==myVec.end()){ //如果每个值都相同,则执行此操作 } 如何使用它来检查向量的向量是否具有所有相等的值?情况就是这样:

 std::vector<std::vector<bool>> my2DVec(3, std::vector<bool>(3));
std::vector my2DVec(3,std::vector(3));

您可以尝试以下方法:

std::vector<std::vector<bool>> my2DVec(3, std::vector<bool>(3));
bool res = std::all_of(my2DVec.begin(), my2DVec.end, [](auto& myVec){
    return std::adjacent_find(myVec.begin(), myVec.end(), std::not_equal_to<>() == myVec.end();
})
std::vector my2DVec(3,std::vector(3));
bool res=std::全部(my2DVec.begin(),my2DVec.end,[](自动和myVec){
返回std::nexting_find(myVec.begin()、myVec.end()、std::not_equal_to()==myVec.end();
})

这应该检查每个向量中的所有元素是否具有相同的值,但不能保证向量具有相同的值

执行相同的操作,而是将其放入loop@NathanOliver类似std::nexting_find(myVec[i].begin(),myVec[i].end(),std::not_equal_to())==myVec[i].end()?obv在一个将i从0增加到2的循环中是的。然后你只需要跟踪那些值是否为false,如果为false,则返回循环外。@抱歉,忘记了最后一部分(顺便说一句,我正在使用该函数尽可能多地保留OP中的代码)