# How to compare two vectors in C++

In this article, we will discuss different ways to compare two vectors in C++.

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

```std::vector<int> vecOfNums1 { 1, 4, 5, 22, 33, 2, 11, 89, 49 };
std::vector<int> vecOfNums2 { 1, 4, 5, 22, 33, 2, 11, 89, 49 };```

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

```/*
* Comparing vectors using operator ==
*/
if (vecOfNums1 == vecOfNums2)
{
std::cout << "matched" << std::endl;
}```

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.

The complete example is as follows,

```#include <iostream>
#include <vector>

int main()
{
std::vector<int> vecOfNums1 { 1, 4, 5, 22, 33, 2, 11, 89, 49 };
std::vector<int> vecOfNums2 { 1, 4, 5, 22, 33, 2, 11, 89, 49 };

/*
* Comparing vectors using operator ==
*/
if (vecOfNums1 == vecOfNums2)
{
std::cout << "matched" << std::endl;
}

return 0;
}```

Output:

`matched`

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

STL Algorithms provide two over loaded versions of equal()

`bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2);`

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()

```#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
std::vector<int> vecOfNums1 { 1, 4, 5, 22, 33, 2, 11, 89, 49 };
std::vector<int> vecOfNums2 { 1, 4, 5, 22, 33, 2, 11, 89, 49 };

// Compare all the elements of two vectors
bool result = std::equal(
vecOfNums1.begin(),
vecOfNums1.end(),
vecOfNums2.begin());

if (result)
{
std::cout << "Both vectors are equal" << std::endl;
}

return 0;
}```

Partial comparison of two vectors using equals()

```#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
/*
* Partial comparison of two vectors using equals()
*/

std::vector<int> vec1{ 1, 4, 5, 22, 33, 2, 11, 89, 49 };
std::vector<int> vec2{ 5, 22, 33};

// Compare 3 elements of vec2 with
// 3 elements in vec1 atrting at index 2.
auto result = std::equal(
vec2.begin(),
vec2.end(),
vec1.begin() + 2);

if (result)
std::cout << "vec1 contains vec2 at position 2 \n";

return 0;
}```

Related Tutorials of C++ Vector

How to Iterate over a Vector in C++?
How to Check if a Value exists in a Vector in C++?
How to find Duplicates in a Vector in C++?

## Comparing two vectors with custom comparators

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

`bool equal (InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, BinaryPredicate pred);`

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.

```//Two vectors of strings
std::vector<std::string> vecOfStr1{ "Hi" , "hello" , "There" , "From" };
std::vector<std::string> vecOfStr2{ "HI" , "HELLO" , "THERE" , "FROM" };```

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

Comparator :

```// Comparator to compare two strings in case insensitive manner
COMPARATOR comparatorObj =
[](STRING_REF left, STRING_REF right){
// Lambda function to compare 2 strings
// in case insensitive manner
return std::equal(
left.begin(),
left.end(),
right.begin(),
[](const char & l, const char & r) {
return (::toupper(l) == ::toupper(r)); });
};```

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

```// Compare two vectors of strings in
// case insensitive manner
auto result = std::equal(
vecOfStr1.begin(),
vecOfStr1.end(),
vecOfStr2.begin(),
comparatorObj);

if (result)
std::cout << "Both vectors are equal" << std::endl;
else
std::cout << "Both vectors are not equal" << std::endl;```

Output:

`Both vectors are equal`

The Complete example is as follows,

```#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>

typedef const std::string& STRING_REF;
typedef std::function< bool (STRING_REF, STRING_REF) > COMPARATOR;

int main()
{
//Two vectors of strings
std::vector<std::string> vecOfStr1{ "Hi" , "hello" , "There" , "From" };
std::vector<std::string> vecOfStr2{ "HI" , "HELLO" , "THERE" , "FROM" };

// Comparator to compare two strings in case insensitive manner
COMPARATOR comparatorObj =
[](STRING_REF left, STRING_REF right){
// Lambda function to compare 2 strings
// in case insensitive manner
return std::equal(
left.begin(),
left.end(),
right.begin(),
[](const char & l, const char & r) {
return (::toupper(l) == ::toupper(r)); });
};

// Compare two vectors of strings in
// case insensitive manner
auto result = std::equal(
vecOfStr1.begin(),
vecOfStr1.end(),
vecOfStr2.begin(),
comparatorObj);

if (result)
std::cout << "Both vectors are equal" << std::endl;
else
std::cout << "Both vectors are not equal" << std::endl;

return 0;
}```

Output

`Both vectors are equal`

## Summary

We learned how to compare two vectors in C++.

### 1 thought on “How to compare two vectors in C++”

1. Does this method cover two dimensional vectors? a vector of vectors?
vector < vector > vector1;
vector < vector > vector2;
equal(vector1.begin(), vector1.end(), vector2.begin());

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top