2013-05-11 128 views
-2
class Medicine{ 
protected: 
    int id; 
    int stock; 
    string name; 
    int concentration; 
public: 
    Medicine(); 
    Medicine(int id,int s,const string n,int c);   
    Medicine(const Medicine& m);     
    Medicine& operator=(const Medicine& m);  
    virtual ~Medicine();  
     int getStock() const; 
    int getConcentration() const;}; 

模块1排序对象

template<typename T> class Vector 
    { 
    private: 
     T* Elems; 
     int Size; 
     int Capacity; 

    public: 
     Vector() 
    ~Vector() 
    void add(T e) 
    void remove(int pos) 
    int getSize() const 
    int getCapacity() const 
    T get(int pos) const;} 

template <typename T> 
    T Vector<T>::get(int pos) const 
    { 
     if (pos < 0 || pos >= this->getSize()) 
      return NULL; 
     return this->Elems[pos]; 
    } 

模块2

class MedRepo : 
public:~MedRepo(); 
void addMed(Medicine s); 
    void upgrade(Medicine s); 
Medicine* findById(int medId) ; 
virtual void removeMed(int id) ; 
int getNrMeds(); 
Vector<Medicine*> getAll() ; 
protected: 
    Vector<Medicine*> MedList; 
}; 

模块3

typedef int (*comparefunction)(const void*, const void*); 

int compareSA(const Medicine* e1, const Medicine* e2){ 
    int q1 = (*(Medicine **) e1)->getStock(); 
    int q2 = (*(Medicine **) e2)->getStock(); 

    if (q1 < q2){ 
      return -1; 
     } 
    else{ 
     if (q1>q2){ 
      return 1; 
     } 
     return 0; 
    } 
} 

模块4

所以阵列,这是我的代码的一部分,我想要做的就是我的对象相对于一些指标分析排序,其中之一是一个在4 module.I've一直与从本周开始类和我已经找到了一些类似的问题,但我不明白,整个过程大约sorting.I've刚读一些有关std:sortstd::stable_sort,但我不知道如何使用它们为我data.All我现在是:

Vector<Medicine*>* Control::sortByStockAsc(){ 
    Vector<Medicine*> all =repo->getAll(); 

任何意见,建议,帮助吗?

+0

不要告诉我这段代码实际上工作... – dtech 2013-05-11 19:30:01

+0

从这个意义上说,我没有错误/ bug直到现在,是的 – 2013-05-11 19:32:05

+1

@MrMime正如我下面所说的,只需通过成员类型你用。更不用说''Medicine'的析构函数,复制构造函数和赋值运算符几乎毫无意义。 – pmr 2013-05-11 19:33:54

回答

3

第一:扔掉你自己的vector类(它通过查看成员的类型有明显的错误 )。使用std::vector<T>

如果你真的想使用它想用std::sort,保持 你将需要实现迭代它。鉴于您刚才 开始与C++,这可能是一个真正的挑战(有很多的文章,全帮手库,并致力于将其书籍章节)。

如果您不想使用std::sort并希望保留自己的 Vector,请为您的容器实施排序算法。

理想的情况:用于医药实现operator<(const Medicine&, const Medicine&)作为一个严格的弱序关系,并用它 这样的:

#include <vector> 
#include <algorithm> 

std::vector<Medicine> mv; 
std::sort(begin(mv), end(mv)); 

如果有更多的可能性比较医学,实现仿函数或免费的功能:

struct LessCmpMedicineByName { 
    bool operator()(const Medicine&, const Medicine&) const; 
}; 
struct LessCmpMedicineById { 
    bool operator()(const Medicine&, const Medicine&) const; 
}; 

// use 
std::sort(begin(mv), end(mv), LessCmpMedicineByName()); 
// or with a lambda 
std::sort(begin(mv), end(mv), [](const Medicine& x, const Medicine& y) { 
    // comparison code here 
    return true; 
}); 
+0

+1,完整和直接的答案:) – LihO 2013-05-11 19:31:28

+0

老师在他所有的例子中都使用了Vector类,如果我实现了这个操作符,那只适用于一种情况,对吗?我需要做1'升序和一个降序排序,用于'Medicine'类的2个不同参数。因此,我也必须实现'operator>',对吗?另外,sort()知道何时使用>或<? – 2013-05-11 19:38:05

+0

@MrMime告诉你老师,'int'不会足够。他正在寻找'size_t'。他也不应该将结尾存储为一个大小,而应该将其作为结尾的指针。如果你实现了两个运算符并且想要在它们之间进行选择,你需要通过传递正确的比较器作为第三个参数来告诉'std :: sort'。 'std :: sort(begin,end,std :: less )'或'std :: sort(begin,end,std :: greater )''。或者,再次使用lambdas。 – pmr 2013-05-11 19:42:28

0

不要扔掉载体。制作自己的容器有很多好处。您必须仔细定义您的排序标准,以便任何排序算法能够正常工作

任何排序操作需要只是>操作者工作的标准必须具有以下性质:

for any a b and c, if a > b and b > c then it must be that a > c 

for any a, a > a must be false. 

平等定义为:

(!(a > b) && !(b > a)) 

的相等条件可以(也应该)编码到你的排序算法中。您无需为您的排序标准专门定义相等运算符。

定期排序算法不需要保存您输入的顺序,如果他们是平等的。在稳定的排序算法中,输入中相同的任何输入都必须以相同的相对顺序输出。例如。如果您只按数字排序玩牌,而您的输入是5小时4天5秒,稳定排序的输出必须是4天5小时 5秒。对于非稳定排序,它可以输出4d 5s 5h。

稳定排序有点慢。你通常不需要稳定的排序。

+0

如果你做错了,你自己的容器的优点是什么 – pmr 2013-05-11 19:43:06

+0

显然没有好处它错了。 – 2013-05-11 19:48:09

+0

@RafaelBaptista我该如何正确呢? – 2013-05-12 15:37:10