The idea that “some compilers don’t work properly” is absurd.
If you’re comparing C-style strings via char*
pointers, then the =
, !=
, <=
, … operators compare the pointers, not the strings they point to. In that case, use the strcmp()
function instead. But since you’re asking about the compare
function, you’re clearly not asking about C-style strings, but about C++-style std::string
objects.
If you’re comparing std::string objects, the equality and comparison operators will work correctly. Any runtime library bug that caused them to fail would be caught before the the implementation goes out the door.
The compare
function provides a bit more functionality. Specifically, it lets you specify a substring of one of the strings being compared rather than the entire string.
Read the documentation (for example, this page on cppreference.com) and decide whether you need that added functionality. If you don’t, then the ==
, !=
, <=
, <
, >=
, >
operators on std::string
objects will work just fine. Their behavior is actually defined in terms of the compare()
function.
There are also some locale-specific issues, but there’s probably no need for you to be concerned about those just yet.
6
solved Why to use compare() for strings?