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

C++VectorOperator OverloadingEquality

C++ Problem Overview


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 

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

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.

Solution 3 - C++

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][1] 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

[1]: https://stackoverflow.com/questions/17394149/how-to-efficiently-compare-vectors-with-c/17394298#17394298 "How to efficiently compare vectors with C++?, by Stefan"

Solution 4 - C++

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

Solution 5 - C++

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.

Solution 6 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsuman shadowView Question on Stackoverflow
Solution 1 - C++Andy ProwlView Answer on Stackoverflow
Solution 2 - C++Joseph MansfieldView Answer on Stackoverflow
Solution 3 - C++TonyView Answer on Stackoverflow
Solution 4 - C++taocpView Answer on Stackoverflow
Solution 5 - C++huskerchadView Answer on Stackoverflow
Solution 6 - C++Mats PeterssonView Answer on Stackoverflow