Is there a string compareTo() ?

Hi!

I'm wondering if there's a similar function to the Java String::compareTo() function?? E.g. one that will not only compare the 2 strings but also take into account the alphabetic ordering.

If you're not familiar with this, imagine comparing 2 strings
1
2
3
4
string str1 = "Hello";
string str2 = "Yo!";

int result = str1.compareTo(str2);


Result will become a negative value since "Hello" comes before "Yo" in an alphabetic order. Comparing str2.compareTo(str1) would generate a positive value.

Does this exists in C++?
str1 > str2 would evaluate to false.
strcmp for c-style strings
std::compare for std::string objects and c-style strings
Last edited on
1
2
3
4
string str1 = "Hello";
string str2 = "Yo!";

int result = str1.compare(str2);


http://www.cplusplus.com/reference/string/string/compare/ ?
Oh! That simple. Thanks :)
Topic archived. No new replies allowed.