1

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

3
  • 1
    Is this an assignment, or are you looking for a good C++ solution to the general problem? Commented Apr 25, 2013 at 17:48
  • 3
    Use std::sort and std::vector<std::string>, obviously. Commented Apr 25, 2013 at 17:49
  • If you have to use qsort, don't use std::string. If you really must, use pointers to std::string in your array. Commented Apr 25, 2013 at 18:43

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

5 Comments

It's a requirement for the assignment that I use qsort, and you can't use qsort on vectors, or so I've read.
@Kyle Unfortunately you cannot use qsort on array of strings as well, thru if you really bound to qsort, you can store pointers in array, and provide comparison function that will compare actual strings but qsort itself will be sorting pointers.
@Kyle: Of course you can use qsort on a vector, if you really want to; the objects in a vector are stored in an array. (Of course, you wouldn't want to since std::sort is safer and probably faster). But as the answer says, you can't use it to sort an array of std::string. If your assignment requires you to do that, then it's asking for the impossible.
@Kyle: Kindly refer your instructor to Section 25.5, paragraph 4, page 877 this document: open-std.org/JTC1/SC22/WG21/docs/papers/2012/n3485.pdf -- It is the current draft of the official C++ standard. Referred to from this site: isocpp.org -- The official C++ website. For a less formal rebuttal, see the "Notes" section here: en.cppreference.com/w/cpp/algorithm/qsort
There is nothing wrong with plain array if size is fixed. Just use sort(begin(plain_array),end(plain_array));
-1

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);
}

2 Comments

I tried sizeof(string) and it doesn't do anything. All it does is display the words in the order I entered them.
take a look at an example of a string sort function i used in conjunction with qsort()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.