[C++] Which string comes first?

Started by
2 comments, last by DevFred 13 years, 11 months ago
Ive I have two std::strings: string 1 : "pa" string 2 : "ap" how do I make a program that compares the two strings and says that string "ap" comes first than "pa"? Another example: string 1: "pas" string 2: "pbs" String 1 comes first because "a" < "b".
Advertisement
std::lexicographical_compare
Also:

string1.compare(string2);

If the result is less than 0, string1 is lexicographically less than string2, and vice versa if the result is greater than 0. If the result is 0, they're equal.
Depends on the type. In case of std::string, it's very easy:
if (string1 < string2) ...

If you're using C strings a.k.a char*, you can use:
if (strcmp(string1, string2) < 0) ...

This topic is closed to new replies.

Advertisement