Why is string with less characters greater than one with more?

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.
What Zhuge said, if you print out the decimal value for e, you should get 101 but if you print the value for i you should get 105.

If you want to the longer strings to be greater then you'll need to use string size.

http://www.cplusplus.com/reference/string/string/size/



Ah, so it goes by the ascii table values.

Thanks guys.
Topic archived. No new replies allowed.