Can strcmp() work with strings in c++? -
i have line of code
if(strcmp(ob[i].getbrand(), ob[j].getbrand()) > 0)
and error
error c2664: 'strcmp' : cannot convert parameter 1 'std::string' 'const char *'
does mean strcmp doesnt work strings, instead has convert char?
don't use strcmp
. use std::string::compare
has same behavior strcmp
.
if(ob[i].getbrand().compare(ob[j].getbrand()) > 0)
or better
if(ob[i].getbrand() > ob[j].getbrand())
generally should use std::string::compare
when have test various cases strings different, e.g.
auto cmp = ob[i].getbrand().compare(ob[j].getbrand()); if(cmp == 0) ... else if(cmp > 0) ... else if(cmp < 0) ...
in way have comparison operation on strings once.
however, in case it's somehow apparent have use comparison result in single case (i'm assuming, don't know context of code given), operator >
suffice, , easier on eye (the brain!).
Comments
Post a Comment