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

58,257

Solution 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.

Solution 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.

Solution 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

Solution 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

Solution 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.

Share:
58,257
suman shadow
Author by

suman shadow

Updated on December 20, 2020

Comments

  • suman shadow
    suman shadow over 3 years

    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