string stringArray[] = { "banana","Banana","zebra","apple","Apple","Zebra","cayote" };
This means that you get a bunch of std::string objects, created from char const*s resulting from the individual string literals.
Consider a single std::string initialisation:
std::string s = "...";
The literal on the right is of type char const[4]. It "decays" to a char const* used by std::string's constructor.
The same happens when you initialize an array of std::string objects from string literals.
cout << (stringArray[1] < stringArray[0]) << endl;
For std::string, using the < operator means lexicographical comparison. Therefore, this uses lexicographical comparison and has the expected result.
cout << ("Banana" < "banana") << endl;
In this case, there is no std::string involved. You compare two char const[7] with each other.
What does this mean? A completely different thing, as it turns out. Both arrays "decay" to a char const* to their first element. The result of comparing two unrelated pointers with < is unspecified. You are lucky to receive 0 as a result, because you may as well receive 1 and not notice the error. Compilers can also generate a warning for this, for example:
warning: comparison with string literal results in unspecified behaviour
So, as you can see, this operation has absolutely nothing to do with lexicographical comparison.
One way to solve this problem is to turn at least one of the operands into an std::string:
cout << (string("Banana") < "banana") << endl;
A < comparison between an std::string and a char const* (or vice versa) is defined to be lexicographical.
strcmpto compare strings.cout << (std::string("Banana") < std::string("banana")) << endl;strncmpdoes not work withstd::string, unless you do the following:strncmp(stringArray[1].c_str(), stringArray[0].c_str()).std::stringand its comparison operator overloads and thecomparemember function.