This post will check if two vectors are equal or not in C++.

Two vectors are said to be equal if they have the same contents in the same order. If two vectors have the same contents but in a different order, they are not equal to each other as results of the [] operator varies. There are many ways to check two vectors for equality in C++, which are discussed below. To check if two vectors contain the same contents but in a different order, sort both vectors before calling any of the following methods.

1. Using == operator

The simplest solution is to use the == operator that checks if the contents of the two containers are equal or not.

Download  Run Code

Output:

Both vectors are not equal

2. Using std::equal function

We can also use the std::equal algorithm to determine whether elements in the two ranges are equal. This might fail if elements in the second sequence are more than in the first sequence. So, it is better to check if the size of both vectors is also the same.

Download  Run Code

Output:

Both vectors are not equal

3. Using std::mismatch function

Finally, the standard library offers the std::mismatch algorithm, which compares the elements in two specified range and returns a pair specifying the first position where they differ.

Download  Run Code

Output:

Both vectors are not equal

That’s all about determining whether two vectors are equal or not in C++.