Compare Arrays in C++

  1. Use the for Loop Statement to Compare Arrays in C++
  2. Use Custom Defined Function to Compare Arrays in C++
  3. Use the std::equal Algorithm to Compare Arrays in C++

This article will demonstrate multiple methods of how to compare arrays in C++.

Use the for Loop Statement to Compare Arrays in C++

In these examples, we will utilize a variable array container - std::vector. This class has a built-in operator == that we can use to compare the two given vectors’ contents. In this case, we need not worry about different vector lengths, as they are handled by the method internally. The return value is boolean with true, implying two vector equality.

Note that we evaluate the expression with ? : conditional statement and printing out the corresponding string messages to the console.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::vector;

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec2 = {12, 32, 43, 53, 23, 25};
    vector<int> i_vec3 = {12, 32, 43, 53, 23, 65};

    i_vec1 == i_vec2 ?
        cout << "Vectors i_vec1 and i_vec2 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec2 are not the same" << endl;

    i_vec1 == i_vec3 ?
        cout << "Vectors i_vec1 and i_vec3 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec3 are not the same" << endl;

    return EXIT_SUCCESS;
}

Output:

Vectors i_vec1 and i_vec2 are not the same
Vectors i_vec1 and i_vec3 are the same

Use Custom Defined Function to Compare Arrays in C++

The previous method can be generalized in the user’s template function, and if needed, custom return codes and exception handling can be added. In this example, we implemented the compareVectorContents function that takes two vector references and evaluates the equality condition using the if structure.

#include <iostream>
#include <vector>

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector;

template<typename T>
bool compareVectorContents(vector<T> &vec1, vector<T> &vec2)
{
    if (vec1 == vec2) {
        return true;
    } else {
        return false;
    }
}

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec3 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec4 = {12, 32, 43};

    compareVectorContents(i_vec1, i_vec3) ?
        cout << "Vectors i_vec1 and i_vec3 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec3 are not the same" << endl;

    compareVectorContents(i_vec1, i_vec4) ?
        cout << "Vectors i_vec1 and i_vec4 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec4 are not the same" << endl;

    return EXIT_SUCCESS;
}
Vectors i_vec1 and i_vec3 are the same
Vectors i_vec1 and i_vec4 are not the same

Use the std::equal Algorithm to Compare Arrays in C++

Another method to compare contents of two vectors is std::equal algorithm from the C++ standard library, defined in the <algorithm> header file. equal method takes 4 parameters representing 2 separate range that need to be compared, thus providing with a more generic interface to deal with comparisons. This method can be employed on ranges formed with iterators from unordered containers, namely - std::unordered_set, std::unordered_map etc. The equal return value is boolean and returns true if the elements are the same in two ranges. Also, the function will return false if the passed ranges differ in lengths.

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

using std::cout; using std::cin;
using std::endl; using std::string;
using std::vector; using std::equal;

int main() {
    vector<int> i_vec1 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec2 = {12, 32, 43, 53, 23, 65};
    vector<int> i_vec3 = {12, 32, 43};

    equal(i_vec1.begin(), i_vec1.end(), i_vec2.begin(), i_vec2.end()) ?
        cout << "Vectors i_vec1 and i_vec2 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec2 are not the same" << endl;
    equal(i_vec1.begin(), i_vec1.end(), i_vec3.begin(), i_vec3.end()) ?
        cout << "Vectors i_vec1 and i_vec3 are the same" << endl :
        cout << "Vectors i_vec1 and i_vec3 are not the same" << endl;

    return EXIT_SUCCESS;
}

Output:

Vectors i_vec1 and i_vec2 are the same
Vectors i_vec1 and i_vec3 are not the same

Related Article - C++ Array

  • Copy an Array in C++
  • Shift Elements in Array in C++