C++ : How to compare two vectors | std::equal() & Comparators
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.
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.
1 2 3 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.
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()
1 |
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()
1 2 3 4 5 |
// 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()
1 2 3 4 5 6 7 8 9 10 11 12 13 |
/* * 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,
1 |
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.
1 2 3 |
//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 :
1 2 3 4 5 6 7 |
// 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.
1 2 3 4 5 6 7 8 |
// 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:
1 |
Both vectors are equal |
Complete example is as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
#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
1 2 3 4 |
matched Both vectors are equal vec1 contains vec2 at position 2 Both vectors are equal |
Follow @thisPtr
Leave a Reply