function
<set>

std::relational operators (set)

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

The equality comparison (operator==) is performed by first comparing sizes, and if they match, the elements are compared sequentially using operator==, stopping at the first mismatch (as if using algorithm equal).

The less-than comparison (operator<) behaves as if using algorithm lexicographical_compare, which compares the elements sequentially using operator< in a reciprocal manner (i.e., checking both a<b and b<a) and stopping at the first occurrence.

The other operations also use the operators == and < internally to compare the elements, behaving as if the following equivalent operations were performed:
operationequivalent operation
a!=b!(a==b)
a>bb<a
a<=b!(b<a)
a>=b!(a<b)

Notice that none of these operations take into consideration the internal comparison object of neither container.

These operators are overloaded in header <set>.

Parameters

lhs, rhs
set containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (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
// set comparisons
#include <iostream>
#include <set>

int main ()
{
  std::set<int> foo,bar;
  foo.insert(10);
  bar.insert(20);
  bar.insert(30);
  foo.insert(40);

  // foo ({10,40}) vs bar ({20,30}):
  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 less than bar
foo is less than or equal to bar

Return Value

true if the condition holds, and false otherwise.

Complexity

Up to linear in the size of lhs and rhs.
For (1) and (2), constant if the sizes of lhs and rhs differ, and up to linear in that size (equality comparisons) otherwise.
For the others, up to linear in the smaller size (each representing two comparisons with operator<).

Iterator validity

No changes.

Data races

Both containers, lhs and rhs, are accessed.
Concurrently accessing the elements of unmodified set objects is always safe (their elements are immutable).

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

                              1. 7297372428 2018-04-20
                              2. 4929522427 2018-04-20
                              3. 1938132426 2018-04-20
                              4. 2267862425 2018-04-20
                              5. 6177082424 2018-04-20
                              6. 838232423 2018-04-20
                              7. 4694502422 2018-04-20
                              8. 6512882421 2018-04-19
                              9. 4659652420 2018-04-19
                              10. 2967832419 2018-04-19
                              11. 8339042418 2018-04-19
                              12. 8147112417 2018-04-19
                              13. 2774752416 2018-04-19
                              14. 4316132415 2018-04-19
                              15. 6265742414 2018-04-19
                              16. 1875142413 2018-04-19
                              17. 4146552412 2018-04-19
                              18. 8205662411 2018-04-19
                              19. 959982410 2018-04-19
                              20. 7153742409 2018-04-19