Comparison operators
Compares the arguments.
Operator name  Syntax  Overloadable  Prototype examples (for class T)  

Inside class definition  Outside class definition  
equal to  a == b

Yes  bool T::operator ==(const T2 &b) const;  bool operator ==(const T &a, const T2 &b); 
not equal to  a != b

Yes  bool T::operator !=(const T2 &b) const;  bool operator !=(const T &a, const T2 &b); 
less than  a < b

Yes  bool T::operator <(const T2 &b) const;  bool operator <(const T &a, const T2 &b); 
greater than  a > b

Yes  bool T::operator >(const T2 &b) const;  bool operator >(const T &a, const T2 &b); 
less than or equal to  a <= b

Yes  bool T::operator <=(const T2 &b) const;  bool operator <=(const T &a, const T2 &b); 
greater than or equal to  a >= b

Yes  bool T::operator >=(const T2 &b) const;  bool operator >=(const T &a, const T2 &b); 

Contents 
[edit] Explanation
Returns the boolean result of comparison of the values of the arguments, which are not modified.
[edit] Arithmetic comparison operators
For every pair of promoted arithmetic types L
and R
, including enumeration types, the following function signatures participate in overload resolution:
bool operator<(L, R);


bool operator>(L, R);


bool operator<=(L, R);


bool operator>=(L, R);


bool operator==(L, R);


bool operator!=(L, R);


If the operands has arithmetic or enumeration type (scoped or unscoped), usual arithmetic conversions are performed following the rules for arithmetic operators. The values are compared after conversions:
[edit] Example
#include <iostream> int main() { std::cout << std::boolalpha; int n = 1; int n2 = 1; std::cout << " 1 == 1? " << (n == n2) << '\n' << "Comparing two signed values:\n" << " 1 < 1? " << (n < n2) << '\n' << " 1 > 1? " << (n > n2) << '\n'; unsigned int u = 1; std::cout << "Comparing signed and unsigned:\n" << " 1 < 1? " << (n < u) << '\n' << " 1 > 1? " << (n > u) << '\n'; unsigned char uc = 1; std::cout << "Comparing signed and smaller unsigned:\n" << " 1 < 1? " << (n < uc) << '\n' << " 1 > 1? " << (n > uc) << '\n'; }
Output:
1 == 1? false Comparing two signed values: 1 < 1? true 1 > 1? false Comparing signed and unsigned: 1 < 1? false 1 > 1? true Comparing signed and smaller unsigned: 1 < 1? true 1 > 1? false
[edit] Pointer comparison operators
For every type P
which is either pointer to object or pointer to function or std::nullptr_t, and for every type MP
that is a pointer to member object or pointer to member function, the following function signatures participate in overload resolution:
bool operator<(P, P);


bool operator>(P, P);


bool operator<=(P, P);


bool operator>=(P, P);


bool operator==(P, P);


bool operator!=(P, P);


bool operator==(MP, MP);


bool operator!=(MP, MP);


This section is incomplete Reason: n3498 needs to be applied 
Comparison operators can be used to compare two pointers (or pointerstomembers, for operator== and operator!= only), or a pointer and a null pointer constant, or two null pointer constants (but only as long as at least one of them is std::nullptr_t: comparison of NULL and NULL follows arithmetic comparison rules). Pointer conversions (pointer to member conversions if the arguments are pointers to members) and qualification conversions are applied to both operands to obtain the composite pointer type, as follows
Results of comparing two pointers (after conversions) are determined as follows:
p
and q
 then the pointers compare equal: p==q, p<=q, and p>=q return true, while p!=q, p<q, and p>q return false,
p
and q
point to members of the same array a[i] and a[j] or one past the end of the array, they results of comparing the pointers is the same as the result of comparing the indexes: if i<j==true then p<q == true.p
and q
point to nonstatic data members within the same class or different base subobjects within the same derived class, or to their members or subobjects, recursively, and if the pointedto members/subobjects have the same access control (e.g. both public:
), and the class is not a union, then the pointer to the later declared subobject/member compares greater than the pointer to the earlier declared subobject/member. In other words, class members in each of the three access modes are positioned in memory in order of declaration.p
and q
point to members of the same union, they compare equal (typically an explicit conversion to void* is required for one of the operands)[edit] Example
#include <iostream> struct Foo { int n1; int n2; }; union Union { int n; double d; }; int main() { std::cout << std::boolalpha; char a[4] = "abc"; char* p1 = &a[1]; char* p2 = &a[2]; std::cout << "Pointers to array elements: p1 == p2 " << (p1 == p2) << ", p1 < p2 " << (p1 < p2) << '\n'; Foo f; int* p3 = &f.n1; int* p4 = &f.n2; std::cout << "Pointers to members of a class: p3 == p4 " << (p3 == p4) << ", p3 < p4 " << (p3 < p4) << '\n'; Union u; int* p5 = &u.n; double* p6 = &u.d; std::cout << "Pointers to members of a union: p5 == (void*)p6 " << (p5 == (void*)p6) << ", p5 < p6 " << (p5 < (void*)p6) << '\n'; }
Output:
Pointers to array elements: p1 == p2 false, p1 < p2 true Pointers to members of a class: p3 == p4 false, p3 < p4 true Pointers to members of a union: p5 == (void*)p6 true, p5 < p6 false
[edit] Notes
Because these operators group lefttoright, the expression a<b<c is parsed (a<b)<c, and not a<(b<c) or (a<b)&&(b<c).
A common requirement for userdefined operator<
is strict weak ordering. In particular, this is required by the standard algorithms and containers that work with Compare
types: std::sort, std::max_element, std::map, etc.
Although the results of comparing pointers of random origin (e.g. not all pointing to members of the same array) is unspecified, many implementations provide strict total ordering of pointers, e.g. if they are implemented as addresses within continuous virtual address space. Those implementations that do not (e.g. where not all bits of the pointer are part of a memory address and have to be ignored for comparison, or an additional calculation is required or otherwise pointer and integer is not a 1 to 1 relationship), provide a specialization of std::less for pointers that has that guarantee. This makes it possible to use all pointers of random origin as keys in standard associative containers such as std::set or std::map.
For the types that are both EqualityComparable
and LessThanComparable
, the C++ standard library makes a distinction between equality, which is the value of the expression a == b and equivalence, which is the value of the expression !(a < b) && !(b < a).
[edit] Standard library
Comparison operators are overloaded for many classes in the standard library.
checks whether the objects refer to the same type (public member function of std::type_info )


compares two error_code s (function) 

compares error_conditions and error_codes (function) 

lexicographically compares the values in the pair (function template) 

lexicographically compares the values in the tuple (function template) 

compares the contents (public member function of std::bitset )


compares two allocator instances (public member function of std::allocator )


compares to another unique_ptr or with nullptr (function template) 

compares with another shared_ptr or with nullptr (function template) 

compares an std::function with std::nullptr (function template) 

compares two durations (function template) 

compares two time points (function template) 

compares two scoped_allocator_adaptor instances (public member function of std::scoped_allocator_adaptor )


compares the underlying std::type_info objects (public member function of std::type_index )


lexicographically compares two strings (function template) 

equality comparison between locale objects (public member function of std::locale )


lexicographically compares the values in the array (function template) 

lexicographically compares the values in the deque (function template) 

lexicographically compares the values in the forward_list (function template) 

lexicographically compares the values in the list (function template) 

lexicographically compares the values in the vector (function template) 

lexicographically compares the values in the map (function template) 

lexicographically compares the values in the multimap (function template) 

lexicographically compares the values in the set (function template) 

lexicographically compares the values in the multiset (function template) 

compares the values in the unordered_map (function template) 

compares the values in the unordered_multimap (function template) 

compares the values in the unordered_set (function template) 

compares the values in the unordered_multiset (function template) 

lexicographically compares the values in the queue (function template) 

lexicographically compares the values in the stack (function template) 

compares the underlying iterators (function template) 

compares the underlying iterators (function template) 

compares two istream_iterator s (function template) 

compares two istreambuf_iterator s (function template) 

compares two complex numbers or a complex and a scalar (function template) 

compares two valarrays or a valarray with a value (function template) 

compares the internal states of two pseudorandom number engines (function) 

compares two distribution objects (function) 

compares two sub_match objects (function) 

lexicographically compares the values in the two match result (function template) 

compares two regex_iterator s (public member function of std::regex_iterator )


compares two regex_token_iterator s (public member function of std::regex_token_iterator )


compares two thread::id objects (function) 

automatically generates comparison operators based on userdefined operator== and operator< (function template) 
[edit] See also
Common operators  

assignment  increment decrement 
arithmetic  logical  comparison  member access 
other 
a = b 
++a 
+a 
!a 
a == b 
a[b] 
a(...) 
Special operators  
static_cast converts one type to another compatible type 