C++ STL - why use !(w < *i) rather than (w==*i) -


vector<widget> vw; // populate vw sort(vw.begin(), vw.end()); widget w;  vector<widget>::iterator = lower_bound(vw.begin(), vw.end(), w);  if ( (i != vw.end()) && !(w < *i) ) // yes, correct!     // found w in vw 

here understanding:

the return value of *i lower_bound not less of w. in other words, w <= *i

here question, why not directly use following condition checking?

if ( (i != vw.end()) && (w == *i) ) // why not use (w == *i)?     // found w in vw 

thank you

because implicit interface <algorithm> uses sorting , kind of stuff requires < operator defined on data type. if used ==, force developers implement on custom types benefit these functions.

in other words, if make sortable type foo, use functions defined in <algorithm>, need overload < operator.


Comments