Jul 16, 2019 in C Plus Plus
Q:

How to compare two vectors | std::equal() & Comparators

1 Answer

0 votes
Jul 16, 2019

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.

1

2

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.

4

5

6

7

/*

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

Related questions

+1 vote
+1 vote
Jul 16, 2019 in C Plus Plus
+1 vote
+1 vote
Jul 16, 2019 in C Plus Plus
...