function
<list>
std::relational operators (list)
(1) | template <class T, class Alloc> bool operator== (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
---|
(2) | template <class T, class Alloc> bool operator!= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
---|
(3) | template <class T, class Alloc> bool operator< (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
---|
(4) | template <class T, class Alloc> bool operator<= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
---|
(5) | template <class T, class Alloc> bool operator> (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
---|
(6) | template <class T, class Alloc> bool operator>= (const list<T,Alloc>& lhs, const list<T,Alloc>& rhs); |
---|
Relational operators for list
Performs the appropriate comparison operation between the list 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:
operation | equivalent operation |
a!=b | !(a==b) |
a>b | b<a |
a<=b | !(b<a) |
a>=b | !(a<b) |
These operators are overloaded in header <list>.
Parameters
- lhs, rhs
- list containers (to the left- and right-hand side of the operator, respectively), having both the same template parameters (T and Alloc).
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
// list comparisons
#include <iostream>
#include <list>
int main ()
{
std::list<int> a = {10, 20, 30};
std::list<int> b = {10, 20, 30};
std::list<int> c = {30, 20, 10};
if (a==b) std::cout << "a and b are equal\n";
if (b!=c) std::cout << "b and c are not equal\n";
if (b<c) std::cout << "b is less than c\n";
if (c>b) std::cout << "c is greater than b\n";
if (a<=b) std::cout << "a is less than or equal to b\n";
if (a>=b) std::cout << "a is greater than or equal to b\n";
return 0;
}
|
Output:
a and b are equal
b and c are not equal
b is less than c
c is greater than b
a is less than or equal to b
a is greater than or equal to b
|
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.
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
- list::swap
- Swap content (public member function)
- list::unique
- Remove duplicate values (public member function)