68

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 
Saurav Sahu
  • 13,038
  • 6
  • 64
  • 79
suman shadow
  • 793
  • 1
  • 5
  • 6
  • Does this answer your question? [How to compare two vectors for equality element by element in C++?](https://stackoverflow.com/questions/6248044/how-to-compare-two-vectors-for-equality-element-by-element-in-c) – Ciro Santilli OurBigBook.com Dec 02 '19 at 13:34

6 Answers6

99

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.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • 1
    I think the last tiny bit of information neede here would be: if you sort your vector, it works. try this: std::sort(vector.begin(), vector.end()); – AnyOneElse Sep 17 '13 at 11:35
  • @AnyOneElse You shouldn't do that for vector equality testing as the order of a vector's elements matters, two vectors with the same elements but in a different order are not mathematically equal. However, if you wish to treat vector equality in such a way that the order of the elements doesn't matter, sorting them before comparison is a possible solution. – user_ Nov 23 '17 at 22:34
9

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.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324
  • I think it would help to also mention how equality between elements is implemented, but right answer +1 – im so confused May 07 '13 at 15:13
  • @Joseph Mansfield What if the object compose of other objects as its attributes? Will `==` compare recursively down all the way to compare the objects for equality? – user3437460 Aug 06 '16 at 15:51
7

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
Tony
  • 151
  • 1
  • 3
2

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

taocp
  • 23,276
  • 10
  • 49
  • 62
2

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.

huskerchad
  • 1,026
  • 8
  • 9
1

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.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227