Thread: qsort - aid

  1. #1
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294

    qsort - aid

    Code:
    string *array;
    //dynmically allocated
    Code:
    qsort ((void*)array, nNum, sizeof (*array), compare );
    Code:
    int compare(const void* string1, const void* string2)
    {
      return stricmp ( (const char*)string1, (const char*)string2);
    }
    it is not sorting data. Could someone help me
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    array is an array of std::string objects, not an array of char*. PROBABLY something like this (not compiled or tested):

    Code:
    qsort ((void*)array, nNum, sizeof (string), compare );
    Code:
    int compare(const void* string1, const void* string2)
    {
       std::string* s1 = (std::string*)string1;
       std::string* s2 = (std::string*)string2;
       
      return s1->compare(*s2);
    }

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Just use std::sort instead of qsort.

    Code:
    std::sort(array, array+size);
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You cannot cast a string to a const char*. If you want a const char* from a string, use the c_str() function.

    The other posted solutions don't do case-insensitive string compares, so while you should use sort, you will still have to create your own comparison function that uses stricmp.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Wait, you want case-insensitive sorting, right? Give me a moment.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    eat my shorts!
    Join Date
    Apr 2002
    Posts
    294
    Quote Originally Posted by Ancient Dragon
    array is an array of std::string objects, not an array of char*. PROBABLY something like this (not compiled or tested):

    Code:
    qsort ((void*)array, nNum, sizeof (string[0]), compare );
    Code:
    int compare(const void* string1, const void* string2)
    {
       std::string* s1 = (std::string*)string1;
       std::string* s2 = (std::string*)string2;
       
      return s1->compare(*s2);
    }
    it worked, thanks
    but could you explain the return line please?
    Games Reviews Previews Desktop Themes Downloads Paintball Forums Shareware Freeware and much more

    The best in Technology and Gaming News

    www.back2games.com

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There you go:
    Code:
    struct iless
    {
      bool operator()(std::string &lhs, std::string &rhs) {
        return stricmp(lhs.c_str(), rhs.c_str()) < 0;
      }
    };
    
    std::sort(array, array+size, iless());
    Personally I'd do it differently, but this code is clearer.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  3. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 02:22 AM
  4. qsort problems... help!
    By Manseed in forum C Programming
    Replies: 5
    Last Post: 06-21-2003, 04:39 PM
  5. Qsort
    By Tombear in forum C Programming
    Replies: 1
    Last Post: 10-28-2001, 09:06 AM