function template
<unordered_map>

std::operators (unordered_map)

equality (1)
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 );
inequality (2)
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 );
Relational operators for unordered_map
These overloaded global operator functions perform the appropriate equality or inequality comparison operation between the unordered_map containers lhs and rhs.

The procedure for the equality comparison is as follows (stopping at any point if the procedure finds a conclusive answer):
  • First, the sizes are compared.
  • Then, each key in one of the containers is looked for in the other, and if found, their values are compared.

Note that the unordered_map::hash_function and unordered_map::key_eq objects are expected to have the same behavior in both lhs and rhs.

Parameters

lhs, rhs
unordered_map containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (Key, T, Hash, Pred and Alloc).

Return Value

true if the condition holds, and false otherwise.

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// unordered_map comparisons
#include <iostream>
#include <string>
#include <unordered_map>

typedef std::unordered_map<std::string,std::string> stringmap;

int main ()
{
  stringmap a = { {"AAPL","Apple"}, {"MSFT","Microsoft"}, {"GOOG","Google"} };
  stringmap b = { {"MSFT","Microsoft"}, {"GOOG","Google"}, {"AAPL","Apple"} };
  stringmap c = { {"MSFT","Microsoft Corp."}, {"GOOG","Google Inc."}, {"AAPL","Apple Inc."} };

  if (a==b) std::cout << "a and b are equal\n";
  if (b!=c) std::cout << "b and c are not equal\n";

  return 0;
}

Output:
a and b are equal
b and c are not equal


Complexity

Average case: linear in size.
Worst case: quadratic in size.

Iterator validity

No changes.

See also