Again, there are lots of functions. Most people use strcmp I guess. I couldn't find the answer in the documentation for strcmp, so I wrote a little program to try it:
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include <set>
void OutputCompare(const std::string& s1, const std::string& s2);
int main()
{
std::string string1 = "Ha";
std::string string2 = "Hallo";
std::string string3 = "He";
std::string string4 = "Hello";
std::string string5 = "Hi";
std::string string6 = "Hillo";
std::cout << "strcmp sort:" << std::endl;
OutputCompare(string1, string2);
OutputCompare(string1, string3);
OutputCompare(string1, string4);
OutputCompare(string1, string5);
OutputCompare(string1, string6);
OutputCompare(string2, string3);
OutputCompare(string2, string4);
OutputCompare(string2, string5);
OutputCompare(string2, string6);
OutputCompare(string3, string4);
OutputCompare(string3, string5);
OutputCompare(string3, string6);
OutputCompare(string4, string5);
OutputCompare(string4, string6);
OutputCompare(string5, string6);
std::set<std::string> wordSet;
wordSet.insert(string1);
wordSet.insert(string2);
wordSet.insert(string3);
wordSet.insert(string4);
wordSet.insert(string5);
wordSet.insert(string6);
std::cout << "\nSet sort:" << std::endl;
std::set<std::string>::const_iterator iterString = wordSet.begin();
std::set<std::string>::const_iterator iterEnd = wordSet.end();
for (; iterString != iterEnd; ++iterString)
std::cout << *iterString << std::endl;
}
void OutputCompare(const std::string& s1, const std::string& s2)
{
if (strcmp(s1.c_str(), s2.c_str()) < 0)
std::cout << std::setw(5) << s1 << " is less than " << s2 << std::endl;
else if (strcmp(s1.c_str(), s2.c_str()) > 0)
std::cout << std::setw(5) << s2 << " is less than " << s1 << std::endl;
else
std::cout << std::setw(5) << s1 << " is equivalent to " << s2 << std::endl;
}
Note the order of the strings in the code is the order they are sorted with strcmp, and even though I used std::string, I still compared c-style strings by using the .c_str() function.
So the longer string is NOT always the greater one with this compare function. It just goes one letter at a time.