Trying to use qsort with vector

后端 未结 1 896
北海茫月
北海茫月 2021-01-11 10: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

1条回答
  •  伪装坚强ぢ
    2021-01-11 10:41

    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).

    0 讨论(0)
提交回复
热议问题