0

はい、私はこれが繰り返しの質問であることを知っており、私が探しているアンサーがここにあることをすでに知っています:

オブジェクトのプロパティによるオブジェクトのベクトルのソート

ただし、これを自分のコードに変換する際に問題があります。上記の質問からこのコード スニペットを見ています。

struct SortByX
{
    bool operator() const(MyClass const& L, MyClass const& R) {
        return L.x < R.x;
    }
};

std::sort(vec.begin(), vec.end(), SortByX();

私が理解していないのはMyClass const & L、 、および によって表されているものですMyClass const & R。そして、これを自分のコードに適用する方法を理解していません。

もう少し詳しく説明すると、( 、 、 、string)doubledoubleパラメーターを持つオブジェクトのベクトルのラッパー クラスに 3 つの並べ替えメソッドを入れています。全体的な目標は、、 、および 3 つの double のうちのいずれかでソートすることです。doubleboolvectorstringbool

これは私が持っている最新のバージョンです:

void StationVector::sortByGrade(int kindOfGas) {
struct SortByGrade {
    int kindOfGas;

    SortByGrade(int kindOfGas) :
            kindOfGas(kindOfGas) {
    }

    bool operator()(GasStation const &L, GasStation const & R) const {
        return L.getPrice(kindOfGas) < R.getPrice(kindOfGas);
    }
};

std::sort(localStations.begin(), localStations.end(),
        SortByGrade(kindOfGas));
}

SortByGrade(kindOfGas))は私に次のエラーを与えます:

`sort(__gnu_cxx::__normal_iterator > >, __gnu_cxx::__normal_iterator > >, model::StationVector::sortByGrade(int)::SortByGrade)' の呼び出しに一致する関数がありません

4

2 に答える 2

2

SortByX二項述語ファンクターです。二項述語は、2 つの引数を取り、ブール値を返すことを意味します。Functor は、そのインスタンスが呼び出し可能であることを意味します。例えば:

MyClass a = ....;
MyClass b = ....;
SortByX comp;
bool flag = comp(a,b); // call this SortByX instance with two MyClass instances

ここで、そのベクトルをソートするために必要なの要素間の比較を実行するために、渡しstd::sortた のインスタンスのコピーを内部的に使用します。SortByXstd::vector<MyClass>

std::vector<MyClass> v;
// fill the vector with MyClass objects
std::sort(v.begin(), v.end(), SortByX()); // sort the vector using a SortByX instance for comparisons

注: これが機能するには、二項述語が厳密な弱い順序付けを実装する必要があります。

于 2013-02-10T22:45:00.920 に答える
1

私が理解していないのは、MyClass const & L、および MyClass const & R で表現されているものです。

Lと、この場合、比較されるコンテナーからのR2 つの項目 (クラスのインスタンス)であり、より小さい演算子の左側と右側にあります。const-reference によって渡されます。MyClassLR

そして、これを自分のコードに適用する方法を理解していません。

あなた自身のbool operator() const(MyClass const& L, MyClass const& R)では、質問で言及した 3 つのデータ メンバーを比較する必要があります。が「未満」の場合とそうでないtrue場合を返します。LRfalse


質問の更新に続いて...

functorに変数を渡したいようです。これを行うには、このSSCCE (ここでコンパイルされます)のようなコンストラクターを作成します。

#include <algorithm>
#include <vector>

namespace model {

struct GasStation
{
    double getprice(int kindOfGas) const
    {
        return kindOfGas;
    }
};

struct StationVector
{
    std::vector<GasStation> localStations;

    struct SortByGrade
    {
        int kindOfGas_;
        SortByGrade(int kindOfGas)
            :kindOfGas_(kindOfGas)
        {
        }

        bool operator() (GasStation const &L, GasStation const & R) const
        { 
            // You'll need other comparisons here, but this is a good start...
            return L.getprice(kindOfGas_) < R.getprice(kindOfGas_); 
        }
    };

    void sortByGrade(int kindOfGas) 
    {
        std::sort(localStations.begin(), localStations.end(), SortByGrade(kindOfGas));
    }
};

}

int main()
{
    model::StationVector sv;
    sv.sortByGrade(0);
}

注:const修飾子は、メソッド名の後ではなく、引数リストの後に来ます。

また、メソッド全体を 1 行にまとめないでください。非常に読みにくくなります。

于 2013-02-10T22:47:53.053 に答える