Kyle
Kyle

Reputation: 21

qsort in C++ for string arrays?

I'm working on a program that takes a list of words entered by the user, ignores the cases (upper and lower) and then sorts them using the function qsort. I'm having an issue with qsort in that I don't know what to pass as the 3rd item qsort(array, sizeOfArray, ??, funcCompare). Can somebody point me in the right direction?

using namespace std;

int compare(const void* , const void*);

const int SIZE = 100;
void main()
{

int i = 0;
int s = 0;
size_t size = 0;
string words;
string list[SIZE];
for (i = 0; i < SIZE; i++)
{
    cout << "Please enter a word. Press ^Z to quit: " << endl;
    cin >> words;
    transform(words.begin(), words.end(), words.begin(), ::tolower);
    if (words.length() > size)
    {
        size = words.length();
    }
    list[i] = words;
    if (cin.eof())
    {
        s = i;
        break;
    }
}
qsort(list, s, ?? , compare);
for (int j = 0; j < i; j++)
    {
        cout << list[j] << endl;
    }   
}

int compare(const void* p1, const void *p2)
{
char char1, char2;

 char1 = *(char *)p1;  // cast from pointer to void
 char2 = *(char *)p2;  // to pointer to int

 if(char1 < char2)
     return -1;
 else
 if (char1 == char2)
    return 0;
 else
    return 1;
 }

The spot in question in qsort has the '??' Any help you can give is appreciated!

This is an assignment

Upvotes: 1

Views: 2253

Answers (2)

Syntactic Fructose
Syntactic Fructose

Reputation: 20144

You need to pass the size of each element in the array, in bytes.

This is accomplished by: sizeof(string)

qsort(list, s, sizeof(string), compare);

EDIT: Take a look at alexrider's post for more information on this


I wrote a qsort string comparision function a while back for BRL-CAD, here is the compare function used(keep in mind written in C, could be optimized).

 * Sort function called by quick sort to sort data according
 * to its second field in the string
 */
int
sort(const void* a, const void* b)
{
    char *ia = *(char**)a;
    char *ib = *(char**)b;

    char Str[MAX_RESULT_LEN];
    char Str2[MAX_RESULT_LEN];

    //get string into array
    GetStr(ia, Str);
    GetStr(ib, Str2);

    int n1 = atoi(Str);
    int n2 = atoi(Str2);
    return (n2 - n1);
}

Upvotes: -1

alexrider
alexrider

Reputation: 4463

Technically you need to pass sizeof(string)
But std::string is not trivial type and thus you are not allowed to use qsort to sort array of strings.

25.5 C library algorithms

4 The function signature:
qsort(void *, size_t, size_t, int (*)(const void *, const void ));
is replaced by the two declarations:
extern "C" void qsort(void
base, size_t nmemb, size_t size, int (compar)(const void, const void*));
extern "C++" void qsort(void* base, size_t nmemb, size_t size, int (compar)(const void, const void*));
both of which have the same behavior as the original declaration. The behavior is undefined unless the objects in the array pointed to by base are of trivial type.

If you are using C++ and std::string you should use also std::vector instead of plain array and std::sort instead of qsort.

Upvotes: 3

Related Questions