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.

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.

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

// 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;

Partial comparison of two vectors using equals()
/*
 * 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.
result = std::equal(vec2.begin(), vec2.end(), vec1.begin() + 2);

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

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
std::function< bool (const std::string & , const std::string &) > comparator = [](const std::string & left, const std::string & 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
result = std::equal(vecOfStr1.begin(), vecOfStr1.end(), vecOfStr2.begin(), comparator);


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

Complete example is as follows,
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
#include <string>

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;
	}

	/*
		Comparing two vectors using std::equal()
	*/

	// 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;


	/*
	 * 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.
	result = std::equal(vec2.begin(), vec2.end(), vec1.begin() + 2);

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

	/*
	 * Comparing two vectors using custom comparators
	 */


	//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
	std::function< bool (const std::string & , const std::string &) > comparator = [](const std::string & left, const std::string & 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
	result = std::equal(vecOfStr1.begin(), vecOfStr1.end(), vecOfStr2.begin(), comparator);


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

	return 0;
}


Output
matched
Both vectors are equal
vec1 contains vec2 at position 2 
Both vectors are equal