In this article we will discuss different ways to compare two vectors.

## Comparing two vectors using operator ==

std::vector provides an equality comparison operator==, it can be used to compare the contents of two vectors. For each element in the vector it will call operator == on the elements for comparisons.
Let’s see how to do that,

Suppose we have 2 vectors of int i.e.

Let’s compare 2 vector using operator == i.e.

If contents of vectors are equal than it will return true else false.

Using operator == for comparing vectors has two limitations i.e.

• It will compare all the elements in vector, we can not compare the sub sections of vectors using it.
• It will compare all the elements in vector, by calling operator == on each element. You can not use custom comparators with it.

As operator == provides limited functionality for comparing two vectors. Therefore, std::equals() comes to rescue.

## Comparing two vectors using STL Algorithm std::equal()

STL Algorithms provide two over loaded versions of equal()

It accepts two ranges and compare all the elements in range 1 i.e. [first1, last1) with all the elements in range 2 starting at first2.
It returns true if all the range1 is equal to all the elements in range2.

Let’s use this,

Compare all elements in two vectors using std::equal()

Partial comparison of two vectors using equals()

## Comparing two vectors with custom comparators

Other over loaded version of std::equals() is as follows,

It compares all the elements in range 1 and range 2 using given binary predicate i.e. comparator.
Let’s see how to use this,

Suppose we have two vectors of strings i.e.

Now let’s compare two vectors of strings in case insensitive manner by passing a custom comparator in equals() i.e.

Comparator :

Comparing 2 vectors using std::equals() and comparator i.e.

Output:

Complete example is as follows,

Output