Check if two vectors are equal in C++

In this article, we will discuss different ways to check if two vectors are equal or not.

Table Of Contents

Method 1: Using == operator

We can directly compare two vectors using == operator to confirm if all elements of two vector are equal or not. Let’s see an example,

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> first  {11, 12, 13, 14, 15};
    vector<int> second {11, 12, 13, 14, 15};

    // check if two vectors are equal
    if(first == second) {
        cout<<"Both the Vectors are equal" << endl;
    }
    else {
        cout<<"Both the Vectors are not equal" << endl;
    }
    return 0;
}

Output:

Both the Vectors are equal

It proved that all the elements of vector are equal.

Advertisements

Method 2: Using STL algorithm equal()

The STL Algorithm equal() accepts two ranges as arguments, and checks if all the elements in both the range are equal or not. So, for passing ranges, we can pass the start, and end iterators of both the vectors, in the equal() function. If it returns True, then it means all the elements in both the vector are equal. Let’s see an example,

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> first  {11, 12, 13, 14, 15};
    vector<int> second {11, 12, 13, 14, 15};

    // check if two vectors are equal
    bool result = equal(first.begin(),
                        first.end(),
                        second.begin(),
                        second.end());

    if(result) {
        cout<<"Both the Vectors are equal" << endl;
    }
    else {
        cout<<"Both the Vectors are not equal" << endl;
    }

    return 0;
}

Output:

Both the Vectors are equal

It proved that all the elements of vector are equal.

Method 3: Using STL Algorithm mismatch()

The STL Algorithm mismatch() accepts two ranges as arguments, and the places in ranges which don’t match. We can pass the start, and end iterators of both the vectors, in the mismatch() function.

It returns a pair of iterators pointing to the first mismatch. The first iterator points into the first vector, the second iterator points into the second vector. If both the iterators in pair points to the end of first, and second vector respectively, then it means that there is no mismatch in the vectors, and all elements of both the vectors are equal. Let’s see an example,

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<int> first  {11, 12, 13, 14, 15};
    vector<int> second {11, 12, 13, 14, 15};

    // check if two vectors are equal
    auto pair = mismatch( first.begin(),
                          first.end(),
                          second.begin());

    if( pair.first  == first.end() && pair.second  == second.end()) {
        cout<<"Both the Vectors are equal" << endl;
    }
    else {
        cout<<"Both the Vectors are not equal" << endl;
    }

    return 0;
}

Output:

Both the Vectors are equal

It proved that all the elements of vector are equal.

Method 4: Using custom function

We can iterate over all elements of a vector by index positions. During iteration, for each element check if the element at the ith index in one vector is equal to the element at ith index in another vector. If this condition evaluates to true for all elements of vector, then it means both the vectors are equal. Let’s see an example,

##include <vector>
#include <iostream>

using namespace std;

template <typename T>
bool is_equal(const vector<T>& first, const vector<T>& second)
{
    bool result = ( first.size() == second.size() );
    if(result)
    {
        for (int i = 0; i < first.size(); i++)
        {
            if(first[i] != second[i])
            {
                result = false;
                break;
            }
        }
    }
    return result;
}


int main()
{
    vector<int> first  {11, 12, 13, 14, 15};
    vector<int> second {11, 12, 13, 14, 15};

    // check if two vectors are equal
    if( is_equal(first, second)) {
        cout<<"Both the Vectors are equal" << endl;
    }
    else {
        cout<<"Both the Vectors are not equal" << endl;
    }

    return 0;
}

Output:

Both the Vectors are equal

It proved that all the elements of vector are equal.

Summary

We leaned about the ways to check if elements of two vector are equal or not in C++.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top