×

C++ STL Tutorial

C++ STL Algorithm

C++ STL Arrays

C++ STL String

C++ STL List

C++ STL Stack

C++ STL Set

C++ STL Queue

C++ STL Vector

C++ STL Map

C++ STL Multimap

C++ STL MISC.

std::equal() in C++

In this article, we are going to see C++ STL function equal() which is used to check whether two sequence ranges have the same contents or not.
Submitted by Radib Kar, on July 15, 2020

equal() as a STL function

The C++ STL equal() function checks whether two sequence ranges have the same contents or not.

Syntax

bool equal(
    InputIterator1 first1, 
    InputIterator1 last1, 
    InputIterator2 first2);

Parameter(s)

  • InputIterator1 first = iterator to start of the first sequence range
  • InputIterator1 last1 = iterator to end of the first sequence range
  • InputIterator2 first2 = iterator to start of the second sequence range

Return type

The return type is bool

  • True - if all elements are the same in both the ranges
  • False - if all elements are not the same in both the ranges

The above syntax is used to compare elements using standard == operator.

We can also define our user-defined binary predicate (instead of '==') for checking whether equal or not. The syntax with user-defined binary predicate is like below:

(Binary predicate is a function which takes two arguments and returns true or false only.)

bool equal(
    InputIterator1 first1, 
    InputIterator1 last1, 
    InputIterator2 first2, 
    BinaryPredicate pred);

Using the above syntax the elements of the corresponding ranges are checked via the predicate.

So, as we found out the last iterator for the second range not being shared as it will compare only the same number of elements as of range 1. Also, it will check sequentially, which means [1,3,5] and [5,3,1] are not the same. So it has to be in the same order for both the range.

1) Using default '==' / '!=' operator

#include <bits/stdc++.h>
using namespace std;

int main()
{
    vector<int> arr1{ 3, 2, 1, 4, 5, 6, 7 };

    vector<int> arr2{ 3, 2, 1, 4, 5 };

    if (equal(arr1.begin(), arr1.begin() + 5, arr2.begin())) {
        cout << "both ranges are exactly equal \n";
    }
    else
        cout << "both ranges are not exactly equal \n";

    vector<int> arr3{ 1, 2, 3, 4, 5, 6, 7 };

    vector<int> arr4{ 1, 2, 3, 4, 5 };

    if (equal(arr3.begin(), arr3.end(), arr4.begin())) {
        cout << "both ranges are exactly equal \n";
    }
    else
        cout << "both ranges are not exactly equal \n";

    return 0;
}

Output

both ranges are exactly equal
both ranges are not exactly equal

Explanation

In the above program, we have checked two cases and have used the default comparator. In the first case,

The first range is arr1.begin() to arr1.begin()+5, i.e., only first five elements of arr1. The second range starts from arr2.begin() and it will check the first five elements only from the starting of range2. Since both are the same thus it's a match.

[3,2,1,4,5]

In the second case since we went for the total range of arr3, thus it was a mismatch.

2) Using user-defined comparator function

Here we have taken a use case where we have two vectors for student details with five students each. By using our user-defined predict we are going to check whether both of the lists are equal or not. Both list are said to be equal if each of the student's details matches. To have a match for student details, all the details (roll, name, score) need to be the same.

#include <bits/stdc++.h>
using namespace std;

class student {

    int score;
    int roll;
    string name;

public:
    student()
    {
        score = 0;
        roll = 0;
        name = "";
    }
    student(int sc, int ro, string nm)
    {
        score = sc;
        roll = ro;
        name = nm;
    }

    int get_score()
    {
        return score;
    }
    int get_roll()
    {
        return roll;
    }
    string get_name()
    {
        return name;
    }
};

bool pred(student a, student b)
{

    //if all details are same return true else false
    if (a.get_name() == b.get_name() && a.get_score() == b.get_score() && a.get_roll() == b.get_roll())
        return true;

    return false;
}

int main()
{

    //1st list
    vector<student> arr1(5);
    //1st student
    arr1[0] = student(80, 5, "XYZ"); //roll 5, marks 80
    //2nd student
    arr1[1] = student(70, 10, "INC"); //roll 10, marks 70
    //3rd student
    arr1[2] = student(85, 7, "HYU"); //roll 7, marks 85
    //4th student
    arr1[3] = student(83, 1, "EFG"); //roll 1,  marks 83
    //5th student
    arr1[4] = student(81, 11, "ABC"); //roll 11,  marks 81

    //2nd list
    vector<student> arr2(5);
    //1st student
    arr2[0] = student(80, 5, "XYZ"); //roll 5, marks 80
    //2nd student
    arr2[1] = student(70, 10, "INC"); //roll 10, marks 70
    //3rd student
    arr2[2] = student(85, 7, "HYU"); //roll 7, marks 85
    //4th student
    arr2[3] = student(83, 1, "EFG"); //roll 1,  marks 83
    //5th student
    arr2[4] = student(81, 11, "ABC"); //roll 11,  marks 81

    //find check both lists are equal or not 
    //based on user-defined predicate
    if (equal(arr1.begin(), arr1.end(), arr2.begin(), pred))
        cout << "Both lists arr1,arr2 are equal\n";
    else
        cout << "Both lists arr1,arr2 are not equal\n";

    //3rd list
    vector<student> arr3(5);
    //1st student
    arr3[0] = student(89, 5, "PVR"); //roll 5, marks 89
    //2nd student
    arr3[1] = student(70, 10, "INC"); //roll 10, marks 70
    //3rd student
    arr3[2] = student(85, 7, "HYU"); //roll 7, marks 85
    //4th student
    arr3[3] = student(83, 1, "EFG"); //roll 1,  marks 83
    //5th student
    arr3[4] = student(81, 11, "ABC"); //roll 11,  marks 81

    //find check both lists are equal or not based 
    //on user-defined predicate
    if (equal(arr1.begin(), arr1.end(), arr3.begin(), pred))
        cout << "Both lists arr1,arr3 are equal\n";
    else
        cout << "Both lists arr1,arr3 are not equal\n";

    return 0;
}

Output

Both lists arr1,arr2 are equal
Both lists arr1,arr3 are not equal

Explanation

Here we have first created two lists with the same elements. Since both lists are of equal size that's why we found equal to return true. For the second case, we have altered an element in arr3 to make unequal and the same reflected in the output. In our user-defined predicate, we have returned true all if all details matched for two students.

So in this article, you saw how efficiently we can use equal to check two ranges are equal or not. An application of this can be checking whether an array is subarray of the other one or not.

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.