1

私は C++ に非常に慣れていないので、私の質問はばかげているように聞こえるかもしれませんが、ベクトルを使用してソート関数に取り組んでいます。

コードはコンパイルして実行できますが、並べ替えはできません。その理由を知ることができますか?

ミッションプラン.cpp

bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
    return t1.locationdata.getCivIndex() > t2.locationdata.getCivIndex();
}

void MissionPlan::topfives()
{   
    topfive.assign( point1.begin(), point1.end() ); 
    sort(topfive.begin(), topfive.end(), sortByCiv);
    for(int i=0; i < 5; i++)
    {
        topfive.at(i).displayPointdata();
    }
}

pointtwod.h

class PointTwoD
{
    private:
        int xcord,ycord;
        float civIndex;
        //LocationData locationdata;

    public:
        PointTwoD();

        PointTwoD(int, int, float);


        string toString();
        void setPointDetail(int x, int y, float civ);
        void displayPointdata();
        void storedata(int, int, float);

        //set/mutator function
        void setxcord(int);
        void setycord(int);

        //get/accessor function
        int getxcord();
        int getycord();
        float getcivIndex();

        LocationData locationdata;


};

Locationdata.h

class LocationData
{
  private:
    string sunType;
    int noOfEarthLikePlanets, noOfEarthLikeMoons;
    float aveParticulateDensity, avePlasmaDensity;
    static float civIndex;

  public:
    LocationData(); //default constructor

    LocationData(string, int, int, float, float); // no default constructor
    void setLocationData(string, int, int, float, float);
    void displaydata();
    string toString();

    //'set' mustator function
    void setsunType(string);
    void setnoOfEarthLikePlanets(int);
    void setnoOfEarthLikeMoons(int);
    void setaveParticulateDensity(float);
    void setavePlasmaDensity(float);

    //'get' accessor function
    string getsunType();
    int getnoOfEarthLikePlanets();
    int getnoOfEarthLikeMoons();
    float getaveParticulateDensity();
    float getavePlasmaDensity();
    static float getCivIndex();

    static float computeCivIndex(string st, int earth, int moons, float particle, float plasma);

};

別の質問があります.. bool myfunction (int i,int j) { return (i

私が期待している結果

X: 4  Y: 9 CIV: 10
X: 1  Y: 2 CIV: 5
X: 5  Y: 4 CIV: 4
X: 6  Y: 1 CIV: 3
X: 10 Y: 6 CIV: 1

私のプログラムから受け取った結果は、入力方法とまったく同じです。

X: 10 Y: 6 CIV: 1
X: 5  Y: 4 CIV: 4
X: 4  Y: 9 CIV: 10
X: 1  Y: 2 CIV: 5
X: 6  Y: 1 CIV: 3
X: 5  Y: 9 CIV: 8
4

1 に答える 1

2

私があなたのコードをひどく間違って読んでいない限り、あなたは間違ったgetCivIndex(). あなたが呼び出しているのは、静的クラス変数を使用しています。これは、LocationData(オブジェクトの内部インスタンスを含む) のすべてのインスタンスが同じPostTwoD静的変数を共有するため、同じ同一の値を持つことを意味します。つまり、コンパレータは常にfalseを返します。これは、N > N が真になることはないためです。したがって、あなたの並べ替えは単に機能しません。

getcivIndex()のインスタンスメンバーを使用したいと思いますPointTwoD; の静的クラスメンバーではありませんLocationData

これを「修正」するには、いくつか変更する必要があります

まず、これを変更します。

float getcivIndex();

これに:(理由はすぐに明らかになります)

float getcivIndex() const;

また、この関数の実装を変更して、constその定義にも追加する必要があります。

次に、これを変更します。

bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
    return t1.locationdata.getCivIndex() > t2.locationdata.getCivIndex();
}

これに:

bool MissionPlan::sortByCiv(const PointTwoD &t1, const PointTwoD &t2)
{
    // use object instance-member. note: this is why getcivIndex()
    //  had to be made const. The t1 and t2 objects are const and as
    //  such only const-member-functions are callable.
    return t1.getcivIndex() > t2.getcivIndex();
}

std::partial_sortまた、シーケンス内の「上位 N」の結果だけが必要な場合は、本格的な並べ替えではなく使用に関する私のコメントを参照することをお勧めします。

于 2013-10-26T10:40:10.860 に答える