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


The C++ unordered_map::operator== function is used to check whether two unordered_maps are equal or not. It returns true if two unordered_maps are equal, else returns false. unordered_map::operator== first checks the size of both unordered_maps, if sizes are same then it compares elements of unordered_maps and stops comparison after first mismatch.

Syntax

template <class Key, class T, class Hash, class Pred, class Alloc>
bool operator== (const unordered_map<Key,T,Hash,Pred,Alloc>& lhs, 
                 const unordered_map<Key,T,Hash,Pred,Alloc>& rhs);

Parameters

lhs First unordered_map.
rhs Second unordered_map.

Return Value

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

Time Complexity

Average Case: Linear i.e, Θ(n).
Worst Case: Quadratic i.e, Θ(n²).

Example:

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

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_map<string, int> uMap1 {{"John", 2500}, {"Jack", 2500}, {"Ella", 2000}};
  unordered_map<string, int> uMap2 {{"Ella", 2000}, {"Jack", 2500}, {"John", 2500}};
  unordered_map<string, int> uMap3 {{"John", 2500}, {"Jack", 2500}, {"Nora", 3000}};

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

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

The output of the above code will be:

uMap1 and uMap2 are equal.
uMap1 and uMap3 are not equal.

❮ C++ - Unordered Map