-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

だから、これは私のコードの一部であり、私がやりたいことは、いくつかの基準に関してオブジェクトをソートすることです。そのうちの1つは4番目のモジュールのものです.私は今週からクラスを扱ってきました. 「似たような質問をいくつか見つけましたが、並べ替えに関するプロセス全体を理解していませんでした。何かを読んだばかりですstd:sortstd::stable_sort、それらを自分のデータに使用する方法がわかりません。今のところ私が持っているのは次のとおりです。

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

アイデア、提案、助けはありますか?

4

2 に答える 2

3

まず、独自のvectorクラスを破棄します (メンバーの型を見れば明らかなバグがあります)。を使用しstd::vector<T>ます。

本当に使い続けて を使いたい場合std::sortイテレータを実装する必要があります。C++ を使い始めたばかりの場合、これは非常に困難な作業になる可能性があります (多くの記事、完全なヘルパー ライブラリ、および 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;
});
于 2013-05-11T19:27:21.797 に答える
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 ))

等値条件は、ソート アルゴリズムにコード化できます (そしてコード化する必要があります)。並べ替え基準に対して等値演算子を具体的に定義する必要はありません。

入力が等しい場合、入力の順序を保持するために、通常の並べ替えアルゴリズムは必要ありません。安定した並べ替えアルゴリズムでは、入力で等しい入力は、出力で同じ相対的な順序で発行される必要があります。たとえば、トランプを番号のみでソートし、入力が 5h 4d 5s の場合、安定ソートの出力は 4d 5h 5s でなければなりません。非安定ソートの場合、4d 5s 5h が出力される可能性があります。

安定ソートは少し遅くなります。多くの場合、安定した並べ替えは必要ありません。

于 2013-05-11T19:40:36.293 に答える