I'm simply trying to figure out why a string with less characters is considered
to be greater than one with more characters. My assumption would be that s3 would be less than s2 since it isn't as long.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
string s1("Hello");
string s2("Hello World");
string s3("Hiya");
if(s3 > s2)
cout << s3 << endl;
return 0;
}
It is using lexographical ordering. Go letter by letter; the 'H' characters are equal, but 'e' < 'i' (you could think of it as 'e' comes before 'i' in the alphabet). This is also how words are ordered in a dictionary. If you want to factor in size, you'll need to compare those yourself.