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
だから、これは私のコードの一部であり、私がやりたいことは、いくつかの基準に関してオブジェクトをソートすることです。そのうちの1つは4番目のモジュールのものです.私は今週からクラスを扱ってきました. 「似たような質問をいくつか見つけましたが、並べ替えに関するプロセス全体を理解していませんでした。何かを読んだばかりですstd:sort
がstd::stable_sort
、それらを自分のデータに使用する方法がわかりません。今のところ私が持っているのは次のとおりです。
Vector<Medicine*>* Control::sortByStockAsc(){
Vector<Medicine*> all =repo->getAll();
アイデア、提案、助けはありますか?