C++ Standard Library C++ STL Library

C++ unordered_multimap - operator== Function



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

Syntax

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

Parameters

lhs First unordered_multimap.
rhs Second unordered_multimap.

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 example below, the operator== function is used to check whether two unordered_multimaps are equal or not.

#include <iostream>
#include <unordered_map>
using namespace std;
 
int main (){
  unordered_multimap<string, string> uMMap1 {{"UK", "London"}, 
                                             {"USA", "New York"}, 
                                             {"USA", "Washington"}};
  unordered_multimap<string, string> uMMap2 {{"USA", "New York"}, 
                                             {"UK", "London"}, 
                                             {"USA", "Washington"}};
  unordered_multimap<string, string> uMMap3 {{"UK", "London"}, 
                                             {"USA", "New York"}, 
                                             {"IND", "Delhi"}};

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

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

The output of the above code will be:

uMMap1 and uMMap2 are equal.
uMMap1 and uMMap3 are not equal.

❮ C++ <unordered_map> Library