function
<map>

std::relational operators (map)

(1)
template <class Key, class T, class Compare, class Alloc>
  bool operator== ( const map<Key,T,Compare,Alloc>& lhs,
                    const map<Key,T,Compare,Alloc>& rhs );
(2)
template <class Key, class T, class Compare, class Alloc>
  bool operator!= ( const map<Key,T,Compare,Alloc>& lhs,
                    const map<Key,T,Compare,Alloc>& rhs );
(3)
template <class Key, class T, class Compare, class Alloc>
  bool operator<  ( const map<Key,T,Compare,Alloc>& lhs,
                    const map<Key,T,Compare,Alloc>& rhs );
(4)
template <class Key, class T, class Compare, class Alloc>
  bool operator<= ( const map<Key,T,Compare,Alloc>& lhs,
                    const map<Key,T,Compare,Alloc>& rhs );
(5)
template <class Key, class T, class Compare, class Alloc>
  bool operator>  ( const map<Key,T,Compare,Alloc>& lhs,
                    const map<Key,T,Compare,Alloc>& rhs );
(6)
template <class Key, class T, class Compare, class Alloc>
  bool operator>= ( const map<Key,T,Compare,Alloc>& lhs,
                    const map<Key,T,Compare,Alloc>& rhs );
Relational operators for map
Performs the appropriate comparison operation between the map containers lhs and rhs.

Operations == and != are performed by first comparing sizes, and if they match, the elements are compared sequentially using algorithm equal, which stops at the first mismatch.

Operations <, >, <= and >= behave as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< reflexively, stopping at the first mismatch.

Notice that none of these operations take into consideration the internal comparison object of neither container, but compare the elements (of type value_type) directly.

value_type is a pair type, and as such, by default, two elements will compare equal only if both their key and mapped value compare equal, and one compare lower than the other only if the first key is lower, or if the keys are equivalent and the mapped value is lower.

These operators are overloaded in header .

Parameters

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

Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// map comparisons
#include <iostream>
#include <map>

int main ()
{
  std::map<char,int> foo,bar;
  foo['a']=100;
  foo['b']=200;
  bar['a']=10;
  bar['z']=1000;

  // foo ({{a,100},{b,200}}) vs bar ({a,10},{z,1000}}):
  if (foo==bar) std::cout << "foo and bar are equal\n";
  if (foo!=bar) std::cout << "foo and bar are not equal\n";
  if (foo< bar) std::cout << "foo is less than bar\n";
  if (foo> bar) std::cout << "foo is greater than bar\n";
  if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
  if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";

  return 0;
}


Output:
foo and bar are not equal
foo is greater than bar
foo is greater than or equal to bar

Return Value

true if the condition holds, and false otherwise.

Complexity

Linear in both lhs and rhs's sizes.

Iterator validity

No changes.

Data races

Both containers, lhs and rhs, are accessed.
Up to all of their contained elements may be accessed.

Exception safety

If the type of the elements supports the appropriate operation with no-throw guarantee, the function never throws exceptions (no-throw guarantee).
In any case, the function cannot modify its arguments.

See also