C++ Standard Library C++ STL Library

C++ unordered_multiset - operator== Function



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

Syntax

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

Parameters

lhs First unordered_multiset.
rhs Second unordered_multiset.

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_multisets are equal or not.

#include <iostream>
#include <unordered_set>
using namespace std;
 
int main (){
  unordered_multiset<int> uMSet1 {10, 20, 30, 100, 30};
  unordered_multiset<int> uMSet2 {10, 20, 100, 30, 30};
  unordered_multiset<int> uMSet3 {10, 20, 30, 500};

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

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

The output of the above code will be:

uMSet1 and uMSet2 are equal.
uMSet1 and uMSet3 are not equal.

❮ C++ <unordered_set> Library