Using STL's qsort function for a vector object

Before I offer any elaboration on the specifics of my problem, I would like to know the answer to the following question:

Is it at all possible to use the STL function - qsort (in the cstdlib header file) - to sort a vector containing class-based objects?

From the literature in the Reference section on this site, the explanation and example given are specifically for arrays. Since a vector is an array capable of changing its size, I thought the qsort could be used. But, I ran into some difficulties!
Yes it is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/* qsort example */
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* qsort */
#include <vector>

std::vector<int> values = { 40, 10, 100, 90, 20, 25 };

int compare (const void * a, const void * b)
{
  return ( *(int*)a - *(int*)b );
}

int main ()
{
  int n;
  qsort (values.data(), 6, sizeof(int), compare);
  for (n=0; n<6; n++)
     printf ("%d ",values[n]);
  return 0;
}
containing class-based objects?

If you do have class Foo, then you simply replace the int with Foo in the above example and change the line 10 to produce <0, 0, >0 for Foos.


"STL" used to mean Standard Template Library and be merely a section of the C++ Standard Library. The functions shared with C Standard Library did not have template code.


Why qsort instead of std::sort?
Thx for your replies. I noted that the example you cited was culled from the qsort function page on here with the array changed to a vector. Let me be more specific now with my problem.

I have a class bookType roughly defined as shown below, followed by a skeletal function main():

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class bookType
{
public:
     fn 1;
     fn 2;
       .
       .
       .
     fn n;
private:
     int a;
     int a[4];
     double d;
     string s;
};


int main()
{
     
     vector<bookType> vec (10);
     
     qsort(vec, 10, sizeof(bookType), myCompare);
     
}


I successfully loaded data from a text file into vec and I do not have a problem with the myCompare function. What the compiler flagged was the first argument of the qsort function, saying:

cannot convert `std::vector<bookType, std::allocator<bookType> >' to `void*' for argument `1' to 
`void qsort(void*, size_t, size_t, int (*)(const void*, const void*))' 


Qn 1: Why is this so since, in a way, vec which is the name of the vector object could be viewed as a pointer like values is in the example on the qsort page?

Qn 2: Since I have a compound data type, in contrast to the fundamental data type used in the example, would I run into problems with the sizeof() operator?

Last edited on
To get a pointer to the first element of vec, use &vec[0] or vec.data().

The behavior of your program will then be undefined because bookType contains a member of type std::string, and is thus not trivially-copyable.

Also note that C's qsort is pretty much a failure, anything you can write yourself would be more efficient, and C++'s std::sort is many times more efficient (and would not have problems with bookType's string member)
vec which is the name of the vector object could be viewed as a pointer like values is in the example on the qsort page

No it can't. The name of a C-style array can be used as if it were a pointer to the first element of the array. A vector is an object - it can't be used as if it were a pointer.

That qsort function is a C standard library function, designed to work with C-style arrays.

EDIT: Ninja'd by Cubbi.
Last edited on
Topic archived. No new replies allowed.