[C++] Which string comes first?
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".
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.
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 you're using C strings a.k.a char*, you can use:
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
Popular Topics
Advertisement