C++ Standard Library C++ STL Library

C++ <array> - operator== Function



The C++ <array> operator== function is used to check whether two arrays are equal or not. It returns true if two arrays are equal, else returns false. operator== compares elements of arrays sequentially and stops comparison after first mismatch.

Syntax

template <class T, size_T N>
bool operator== (const array<T,N>& lhs, const array<T,N>& rhs);

Parameters

lhs First array.
rhs Second array.

Return Value

Returns true if the contents of lhs are equal to the contents of rhs, else returns false.

Time Complexity

Linear i.e, Θ(n).

Example:

In the example below, the operator== function is used to check whether two arrays are equal or not.

#include <iostream>
#include <array>
using namespace std;
 
int main (){
  array<int, 3> arr1 {10, 20, 30};
  array<int, 3> arr2 {10, 20, 30};
  array<int, 3> arr3 {20, 30, 40};

  if (arr1 == arr2)
    cout<<"arr1 and arr2 are equal.\n";
  else
    cout<<"arr1 and arr2 are not equal.\n";

  if (arr1 == arr3)
    cout<<"arr1 and arr3 are equal.\n";
  else
    cout<<"arr1 and arr3 are not equal.\n";
    
  return 0;
}

The output of the above code will be:

arr1 and arr2 are equal.
arr1 and arr3 are not equal.

❮ C++ <array> Library