How to sort a structure pointer vector

I am trying to sort the type concurrent_vector where hits_object:

struct hits_object{
        unsigned long int hash;
        int position;
};

Here is the code I'm using:

concurrent_vector<hits_object*> hits;

for(i=0;...){
    hits_object *obj=(hits_object*)malloc(sizeof(hits_object));
    obj->position=i;
    obj->hash=_prevHash[tid];
    hits[i]=obj;
}

Now I have filled a concurrent_vector<hits_object*>called hits.

But I want to sort this concurrent_vector by the position property !!!

Here is an example of what's inside a typical hit object:

0 1106579628979812621
4237 1978650773053442200
512 3993899825106178560
4749 739461489314544830
1024 1629056397321528633
5261 593672691728388007
1536 5320457688954994196
5773 9017584181485751685
2048 4321435111178287982
6285 7119721556722067586
2560 7464213275487369093
6797 5363778283295017380
3072 255404511111217936
7309 5944699400741478979
3584 1069999863423687408
7821 3050974832468442286
4096 5230358938835592022
8333 5235649807131532071

I want to sort it based on the first column ("position" type int). The second column is a hash of the type unsigned long int.

Now I tried to do the following:

std::sort(hits.begin(),hits.end(),compareByPosition);

where is compareByPositiondefined as:

int compareByPosition(const void *elem1,const void *elem2 )
{
  return ((hits_object*)elem1)->position > ((hits_object*)elem2)->position? 1 : -1;
}

but I keep getting segmentation errors when I put in a string std::sort(hits.begin(),hits.end(),compareByPosition);

Please, help!

+5
source share
5 answers

0 1, 1 -1, :

bool compareByPosition(const hits_object *elem1, const hits_object *elem2 )
{
    return elem1->position < elem2->position;
}

, , std::sort , comp true, , .

. sbi Mike Seymour.

+7

, concurrent_vector, , . , std::vector, hits.push_back(obj), hits[i] = j; [] .

a < b, ; , , . , sort , C-style void *; :

bool compareByPosition(hits_object const * elem1, hits_object const * elem2) {
    return elem1->position < elem2->position;
}

, new (, , malloc) ; vector<hits_object> ( , , ). ( , , , , ), , std::unique_ptr, , delete , .

+5

int (*)(void*, void*) C qsort(). ++ std::sort() :

bool cmp(const hits_object* lhs, const hits_object* rhs)
{
    return lhs->position < rhs->position;
}

std::sort(hits.begin(), hits.end(), &cmp);

, std::pair struct, :

typedef std::pair<int position, unsigned long int hash> hits_object;
// ...
std::sort(hits.begin(), hits.end());
+5

, std::sort(), , , , operator<():

bool is_smaller_position(const hits_object* lhs, const hits_object* rhs)
{
  return lhs->position < rhs->position;
}

, operator<(), , .


: malloc() ++, new. , , , . , concurrent_vector - - std::vector, , . :

concurrent_vector<hits_object*> hits;

for(i=0;...){
    hits_object obj;
    obj.position=i;
    obj.hash=_prevHash[tid];
    hits.push_back(obj);
}
+4

:

for(i=0;...){
    hits_object *obj=(hits_object*)malloc(sizeof(hits_object));
    obj->position=i;
    obj->hash=_prevHash[tid];
    hits[i]=obj;
}

here you are already sorting the array based on "i" because you set the position to i and also become the hit index!

also why use malloc, instead use the new one (/ delete). Then you can create a simple constructor to initialize the hits_object structure

eg.

struct hits_object
{
   int position;
   unsigned int hash;

   hits_object( int p, unsigned int h ) : position(p), hash(h) {;}
};

and then write instead

hits_object* obj = new hits_object( i, _prevHash[tid] );

or even

hits.push_back(  new hits_object( i, _prevHash[tid] ) );

Finally, your comparison function should use the same data type as the vector for its arguments

bool cmp( hits_object* p1, hits_object* p2 )
{
  return p1->position < p2->position;
}
0
source

All Articles