trying to compare strings and having problems

Hi, I have an array of strings and I am trying to organize them in alphabetical order. It isn't working. Can someone please tell me if there is a way to do this and if so how. Thanks. I will post the cryptic error message below.

here is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void sortList(string unorgList[])
{
   string temp;
   for (int i = 0; i < 200; i++)
   {
      for (int j = 1; j < 139; j++)
      {
         if ( strcmp( unorgList[1], unorgList[2]) < 0)
         {
            temp = unorgList[1];
            unorgList[1] = unorgList[2];
            unorgList[1] = temp;
         }
      }
   }
   
   
}


error: cannot convert 'std::basic_string<char, std::char_traits<char>, std::allocator<char> >' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'
Last edited on
strcmp works with c-style strings, not C++ std::strings.

http://www.cplusplus.com/reference/string/basic_string/compare/

Also, the std::sort function works with arrays, you just need to give it the address of the first element and the address of the last element. In this case, however, it would try to sue operator<, which std::string doesn't have, so it won't work in this case. But useful to know for future reference.
it would try to sue operator<, which std::string doesn't have

string has operator<
My professor said that we have to write our own sort function. I can't think of another way to do it besides comparing string sizes. Does anyone else have any ideas? Thanks!
Alphabetical order?
EDIT sorry I skipped the beginning of the OP post

The compare() method gives you information on how the strings are different. I assume it uses the ASCII value of the characters to do that.
Example:
1
2
3
std::string compared = "men";
std::string comparing = "man";
compared.compare(comparing);

here the second character is different. In ASCII 'e' is 0x65 and 'a' is 0x61. Since the compared string's unmatching charater has higher value than the comparing string's, the function returns a value greater than 0.

Also have a look at this for future reference http://www.cplusplus.com/reference/algorithm/swap/
Last edited on
@Cubbi: I didn't know because it wasn't in the reference on this site - whoops.

@familyman:
1
2
3
4
if(MyStdStringB < MyStdStringA)
{
    //swap
}
Exactly what L B did ^
Topic archived. No new replies allowed.