Trying to use qsort with vector
Asked Answered
D

1

16

I'm trying to learn c++ and was trying using sort and qsort. sort() works just fine but qsort doesn't, I don't know why, so can you help me please this is the code I was trying to compile

#include<iostream>
#include<vector>
#include<cstdlib>
#include<ctime>
#include<algorithm>


using namespace std;

int compvar(const void *one, const void *two)
{
    int a = *((int*)one);
    int b = *((int*)two);
    if (a<b)
       return -1;
    if (a == b)
       return 0;
    return 1;   

}

void bvect(vector<int> &vec, int num)
{
     srand(time(NULL));
     for(int i=0; i<num; ++i)
             vec.push_back(rand()%1000 + 1);
}

void showvec(vector<int> vec)
{
     for (int i=0; i<vec.size(); ++i)
         cout<<vec[i]<<endl;
}


int main()
{
    vector<int>numbers;
    bvect(numbers, 1000);
    showvec(numbers);
    qsort(numbers.begin(), numbers.size(), sizeof(int), compvar);
    showvec(numbers);

    return 0;
}
Dioecious answered 6/9, 2012 at 20:54 Comment(11)
Allow me to be the first to advise that you "Just say NO!". Using qsort on a vector is just plain nuts.Ani
Why do you even want to use qsort?!Ricker
You seem to be making the assumption that your implementation uses raw pointers as vector iterators. Does it? Regardless, your code should not assume it does. Use &numbers[0] instead of numbers.begin().Gubernatorial
if you are learning C++, forget about qsort and other C-functionsEchoechoic
use comparator as here cplusplus.com/reference/clibrary/cstdlib/qsort and also you should call srand only once so it would be better if you call it in main functionEchoechoic
int a = *((int*)one); the outermost parentheses aren't needed.Electuary
@Jupiter - the comparator looks just fine. What do you think should be different?Electuary
@Pete Becker "if" statement looks like overhead)Echoechoic
@Jupiter - how would you change it? I don't see much extra there.Electuary
@PeteBecker: The final if (a == b) return 0; return 1; could be replaced by return (a > b); but that is minor compared to the horror of not using std::sort in the first place.Koodoo
@Koodoo - the optimizer will make good sense of all of this.Electuary
L
28

First of all, DON'T.

If you just want to muck about, you can replace iterators with actual pointers:

qsort(&numbers[0], numbers.size(), sizeof(int), compvar);

Apart from not doing all the work std::sort does, there is one unexpected thing about qsort. It is slower.

  1. sort (myvector1.begin(), myvector1.end());

  2. sort (myvector2.begin(), myvector2.end(), myfunction);

  3. sort (myvector3.begin(), myvector3.end(), myobject);

  4. qsort(&myvector4[0], myvector4.size(), sizeof(int), cmyfunction);

4 is the slowest, followed by 2 (function pointer passed to std::sort). 1 and 3 (default and functor) are the fastest (compiled with gnu's g++ with -O3 flag).

Locarno answered 6/9, 2012 at 21:4 Comment(1)
Thanks for the answers.It worked!. I know qsort() is slower than sort() and that's what I was trying to test, I'm taking a programming course and the professor told us to test them both.again thanksDioecious

© 2022 - 2024 — McMap. All rights reserved.