Compare arrays for equality in C++
This post will discuss how to compare arrays for equality in C++.
1. Using == operator
We can easily compare two std::array using the == operator. It checks whether the contents of the two containers are equal or not. Consider the following code demonstrating this:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <array> int main() { std::array<int, 5> first {1, 2, 3, 4, 5}; std::array<int, 5> second {1, 2, 3, 4, 5}; if (first == second) { std::cout << "Both arrays are equal"; } else { std::cout << "Both arrays are not equal"; } return 0; } |
Output:
Both arrays are equal
Note that you can’t compare two C-style arrays using this approach, since it decays to pointers to the first elements of the respective arrays, and we will end up comparing two addresses. The following C++ program demonstrates it:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> int main() { int first[] = {1, 2, 3, 4, 5}; int second[] = {1, 2, 3, 4, 5}; if (first == second) { std::cout << "Both arrays are equal"; } else { std::cout << "Both arrays are not equal"; } return 0; } |
Output:
Both arrays are not equal
2. Using std::equal
Alternatively, we can use the std::equal function to determine if corresponding elements in the specified ranges are equal. The following example shows how to compare two arrays with the std::equal function.
Notice the length check before calling the std::equal function. This is done to avoid incorrect output when the second array contains more elements than in the first array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <algorithm> int main() { int first[] = {1, 2, 3, 4, 5}; int second[] = {1, 2, 3, 4, 5}; int m = sizeof(first) / sizeof(*first); int n = sizeof(second) / sizeof(*second); if (m == n && std::equal(first, first + m, second)) { std::cout << "Both arrays are equal"; } else { std::cout << "Both arrays are not equal"; } return 0; } |
Output:
Both arrays are equal
With C++11, we can pass an iterator to the beginning and end of the array to the std::equal function:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <iostream> #include <iterator> int main() { int first[] = {1, 2, 3, 4, 5}; int second[] = {1, 2, 3, 4, 5}; int m = sizeof(first) / sizeof(*first); int n = sizeof(second) / sizeof(*second); if (m == n && std::equal(std::begin(first), std::end(first), std::begin(second))) { std::cout << "Both arrays are equal"; } else { std::cout << "Both arrays are not equal"; } return 0; } |
Output:
Both arrays are equal
3. Using Custom Logic
Another option is to write custom logic for comparing two arrays in C++. The idea is to iterate with indices and compare the corresponding elements at the respective position in both arrays. Here’s what the code would look like:
|
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 |
#include <iostream> template <typename T, int m, int n> bool isEqual(T (&first)[m], T (&second)[n]) { if (m != n) { return false; } for (int i = 0; i < m; i++){ if (first[i] != second[i]) { return false; } } return true; } int main() { int first[] = {1, 2, 3, 4, 5}; int second[] = {1, 2, 3, 4, 5}; if (isEqual(first, second)) { std::cout << "Both arrays are equal"; } else { std::cout << "Both arrays are not equal"; } return 0; } |
Output:
Both arrays are equal
4. Using memcmp() function
Finally, we can compare two blocks of memory using the memcmp() function. It returns 0 if the contents of both memory blocks are equal, as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <cstring> int main() { char first[] = "C"; char second[] = "C++"; int n = memcmp(first, second, sizeof(first)); if (n == 0) { std::cout << "Both arrays are equal"; } else { std::cout << "Both arrays are not equal"; } return 0; } |
Output:
Both arrays are not equal
That’s all about comparing arrays for equality in C++.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)