Can I use ' == ' to compare two vectors. I tried it and seems to be working fine. But I don't know whether it will work in more complex situations

不打扰是莪最后的温柔 提交于 2019-12-03 05:25:56

问题


First example:

int main(){
    using namespace std;   
    vector<int> v1{10, 20, 30, 40, 50};
    vector<int> v2{10, 20, 30, 40, 50};

    if(v1==v2)
        cout<<"equal";
    else
        cout<<"unequal";
}   // it returns equal 

Second example:

int main(){
    using namespace std;   
    vector<int> v1{10, 20, 30, 40, 50};
    vector<int> v2{10, 20, 100000, 40, 50};

    if(v1==v2)
        cout<<"equal";
    else
        cout<<"unequal";
}   // it returns notequal 

回答1:


The overload of operator == that works on two std::vectors will compare the vector sizes and return false if those are different; if not, it will compare the contents of the vector element-by-element.

If operator == is defined for the vector's element type, then the comparison of vectors through operator == is valid and meaningful.

In formal terms, the C++11 standard specifies the operational semantics of a == b for sequence containers as (Table 96, § 23.2.1):

== is an equivalence relation.

distance(a.begin(), a.end()) == distance(b.begin(), b.end()) && equal(a.begin(), a.end(), b.begin())

As you can see, equality between sequence containers is defined in terms of the std::equal algorithm between ranges defined by pairs of iterators, which in turn uses operator == for comparison of individual elements.




回答2:


Yes, you can use operator== to compare two std::vectors. It will return true only if the vectors are the same size and all elements compare equal.




回答3:


Be advised that vectors are ordered, and std::equal or the == operator check that the vectors have the same contents in the same order. For many use cases this might be enough.

But there might be occasions when you want to know if two vectors have the same contents but not necessarily in the same order. For that case you need another function.

One nice and short implementation is the one below. It was suggested here: https://stackoverflow.com/questions/17394149/how-to-efficiently-compare-vectors-with-c/17394298#17394298 There you will also find a discussion on why you might not want to use it...

Put this in a header file of your choice:

#include <algorithm>

template <class T>
static bool compareVectors(std::vector<T> a, std::vector<T> b)
{
   if (a.size() != b.size())
   {
      return false;
   }
   ::std::sort(a.begin(), a.end());
   ::std::sort(b.begin(), b.end());
   return (a == b);
}

And here an example illustrating the above theory:

std::vector<int> vector1;
std::vector<int> vector2;

vector1.push_back(100);
vector1.push_back(101);
vector1.push_back(102);

vector2.push_back(102);
vector2.push_back(101);
vector2.push_back(100);

if (vector1 == vector2)
   std::cout << "same" << std::endl;
else
   std::cout << "not same" << std::endl;

if (std::equal(vector1.begin(), vector1.end(), vector2.begin()))
   std::cout << "same" << std::endl;
else
   std::cout << "not same" << std::endl;

if (compareVectors(vector1, vector2))
   std::cout << "same" << std::endl;
else
   std::cout << "not same" << std::endl;

The output will be:

not same
not same
same



回答4:


You can check the documentation of operator== for vector: operator==,!=,<,<=,>,>=(std::vector)

Quoting from the link:

 template< class T, class Alloc >
 bool operator==( vector<T,Alloc>& lhs,
             vector<T,Alloc>& rhs );

Compares the contents of two containers.

Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs has equivalent element in rhs at the same position.

parameters:

lhs, rhs containers whose contents to compare

T must meet the requirements of EqualityComparable in order to use versions

Return value

true if the contents of the containers are equivalent, false otherwise




回答5:


Yes. A good reference is cppreference.com, where you can look up operator== for vector<T>, for example on this page: non-member operators, and you will find:

Checks if the contents of lhs and rhs are equal, that is, whether lhs.size() == rhs.size() and each element in lhs has equivalent element in rhs at the same position.




回答6:


As long as your vector contains elements that in themselves can be compared (have operator==), this works, yes. Note however that if you have a vector that contains for example pointers to identical objects, but not the SAME instance of an object, then the vector is not considered identical, because the element in the vector is what is compared, not the contents of the element as such, if that makes sense.



来源:https://stackoverflow.com/questions/16422486/can-i-use-to-compare-two-vectors-i-tried-it-and-seems-to-be-working-fine

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!