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


The C++ array::operator!= function is used to check whether two arrays are unequal or not. It returns true if two arrays are not equal, else returns false. array::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 not equal to the contents of rhs, else returns false.

Time Complexity

Linear i.e, Θ(n).

Example:

In the below example, the array::operator!= function is used to check whether two arrays are unequal 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 not equal.\n";
  else
    cout<<"arr1 and arr2 are equal.\n";

  if (arr1 != arr3)
    cout<<"arr1 and arr3 are not equal.\n";
  else
    cout<<"arr1 and arr3 are 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