Facebook Page Twitter Page LinkedIn Page
× C++ Standard Library C++ STL Library


The C++ algorithm::equal function is used to compare the elements in the range [first1,last1) to the elements in range starting at first2, and returns true if all of the elements in both ranges match. The elements are compared using operator== (in first version) or pred (in second version).

Syntax

//equality version
template <class InputIterator1, class InputIterator2> 
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);

//predicate version
template <class InputIterator1, class InputIterator2, class BinaryPredicate>
  bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);

Parameters

first1 Specify initial position of the input iterator of the first sequence. The range used is [first1,last1).
last1 Specify final position of the input iterator of the first sequence. The range used is [first1,last1).
first2 Specify initial position of the input iterator of the second sequence.
pred A binary predicate that takes two elements as arguments (one of each sequence and in the same order) and returns a value convertible to bool. The returned value indicates whether the arguments are considered to match in context of the function.

Return Value

Returns true if all elements in the range [first1,last1) are compared to equal to the elements in range starting at first2, else returns false.

Time Complexity

Linear i.e, Θ(n).

Example:

In the below example, the algorithm::equal function is used to find out the largest element in the given range.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main (){
  int Arr[] = {10, 20, 30, 40, 50};
  vector<int> vec1 = {10, 20, 30, 40, 50};
  vector<int> vec2 = {20, 30, 40, 50, 60};

  cout<<boolalpha;

  //checking the ranges of Arr and vec1 for equality
  bool retval1 = equal(Arr, Arr+5, vec1.begin());
  cout<<"Are the ranges of Arr and vec1 equal?: "<<retval1;

  //checking the ranges of Arr and vec2 for equality
  bool retval2 = equal(Arr, Arr+5, vec2.begin());
  cout<<"\nAre the ranges of Arr and vec2 equal?: "<<retval2;

  return 0;
}

The output of the above code will be:

Are the ranges of Arr and vec1 equal?: true
Are the ranges of Arr and vec2 equal?: false

Example:

The below example shows how to use pred with algorithm::equal function.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool eq (int i, int j) {
  return (i==j);
}

int main (){
  int Arr[] = {10, 20, 30, 40, 50};
  vector<int> vec1 = {15, 20, 30, 40, 50};
  vector<int> vec2 = {10, 20, 30, 40, 50};

  cout<<boolalpha;

  //checking the ranges of Arr and vec1 for equality
  bool retval1 = equal(Arr, Arr+5, vec1.begin(), eq);
  cout<<"Are the ranges of Arr and vec1 equal?: "<<retval1;

  //checking the ranges of Arr and vec2 for equality
  bool retval2 = equal(Arr, Arr+5, vec2.begin(), eq);
  cout<<"\nAre the ranges of Arr and vec2 equal?: "<<retval2;

  return 0;
}

The output of the above code will be:

Are the ranges of Arr and vec1 equal?: false
Are the ranges of Arr and vec2 equal?: true

❮ C++ <algorithm> Library