Use only <comparisons in general programming

I read the "Templates and General Programming" part in C ++ Primer (5th Edition), but I was a little confused by some things there.

Speaking about "writing independent code" on P655 and P656, the author stated that "Tests in the body use only" comparisons " , because " by writing code using only the <operator, we reduce the requirements for types that can be used with our comparison function These types must support <, but they also do not need support>. .

Are there types that support <but not>? If so, why does <has an edge over>? I searched on Google for a while, but I was not able to get an answer. Can someone give me some examples or links to referrals?

+4
source share
3 answers

If so, why <has an edge over >?

Simple agreement. A symbol '<'in first place in ASCII, and less than over a partially ordered set, is the primitive from which comparisons can be built (i.e., the Set is divided into equivalence groups).

If we have a, band operator <:

  • a <b if operator <( a, b )
  • a> b if operator <( b, a )
  • a ≤ b, ! operator <( b, a )
  • a = b, ! operator <( a, b ) && ! operator <( b, a )
  • .

++ , . , , Concept C-C, , , , .

, <, >?

, . , operator <, >, . - std::relops > <, > .

, , std::sort std::map, >.

+3

(, Haskell) . , (, ) , <=, ( ) :

x > y ! (x <= y)

x == y x <= y && y <= x

x >= y x > y || x == y

x < y ! (x >= y)

. , , , "". (, >) . , .

, < >, , . , . {<=} {>=} . , those types must support , but they need not also support .

+2

, operator < - , . , , API, . , std::map operator <, . , , , (O (log n)). . ( , , operator < , ).

, . . operator <, .

, , operator < operator >, , (, ). 1 5 , 1 < 2 < 3 < 4 < 5. , operator <(T left, T right), true, "" .

And after another passage over the text that you quoted, it seems to me that they are talking more about the person who wrote the algorithm that uses operator <, and not the one who writes the class that implements operator <. If you are writing an algorithm that should work with other people's code, and you want them to provide you with a way to arrange their elements, you only need to operator <. The point is to simplify the work of the "client".

+1
source

Source: https://habr.com/ru/post/1546215/


All Articles