lundi 5 décembre 2016

How to fix this sort?

I am a newbie here and trying to sort a list of pointers of structs named item. But it seems there is always some segment fault. Could you help me to fix it?

#pragma once
#include <algorithm>
#include <mutex>
#include <climits>
class hello {
  private:
    struct item
    {
        int key;
        int order;
    };
    bool cmp(struct item * a, struct item * b){
        return a->key < b->key;
    }  

  public:
    void insert(int* keys, int num) {
        item * allitems[num];
        int i;
        for(i=0;i<num;i++){
            allitems[i] = new item();
            allitems[i]->key=keys[i];
            allitems[i]->order=i;
        }
        std::sort(allitems, allitems+num*sizeof(item*), cmp);
    }
};

Aucun commentaire:

Enregistrer un commentaire