C ++ std :: sort() - 无法对访问成员变量的类类型的向量进行排序

时间:2015-10-31 07:46:58

标签: c++ function class sorting std

Entry.h

//returns the sum of all non mega entry percentages
float sumOfNonMegaEntryPct(vector<Number>& arg1_Numbers);

Entry.cpp

//returns the sum of all mega entry percentages
float Entry::sumOfMegaEntryPct(vector<MegaNumber>& arg1_MegaNumbers)
{
    float sumPct = 0.00f;
     for (MegaNumber c : megaEntry)
     {
         sumPct = sumPct + arg1_MegaNumbers[c.getID()].getOccurencePct();
     }

     return sumPct;
}

Lotto.h

public:
//compares two entries, used for sorting algorithm, sorts by nonmega number
bool compareEntry_sumPct_nonMega(Entry arg1, Entry arg2); 

    protected:
    vector<Numbers> numbers;
    vector<MegaNumbers> megaNumbers;

Lotto.cpp

#include "lotto.h"

//sorts nonmega numbers by sum of their pct, used for sort algorithm
bool Lotto::compareEntry_sumPct_nonMega(Entry arg1, Entry arg2)
{
    bool b = arg1.sumOfNonMegaEntryPct(numbers) < arg2.sumOfNonMegaEntryPct(numbers);
     return b;
}

Source.cpp

vector<Entry> copyGameEntry = game.getPlayEntry();
sort(copyGameEntry.begin(), copyGameEntry.end(),
     bind(&Lotto::compareEntry_sumPct_nonMega, game));

这只是代码的一部分,但我认为它足够有意义。编译时,我得到错误:

  

严重级代码说明项目文件行错误C2451条件   表达类型&#39; std :: _ Unforced&#39;是非法彩票排序e:\程序   files(x86)\ microsoft visual studio 14.0 \ vc \ include \ algorithm 3133

     

严重级代码说明项目文件行错误C2675一元&#39;!&#39;:   &#39;的std :: _非受迫性&#39;没有定义此运算符或转换为   预定义操作员Lottery Sort e:\ program可接受的类型   files(x86)\ microsoft visual studio 14.0 \ vc \ include \ algorithm 3118

问题:

可能是什么问题?

3 个答案:

答案 0 :(得分:6)

您错误地使用了Intent intent = new Intent(MainActivity.this, SecondActivity.class); intent.putExtra("parameter1", num2); startActivity(intent); 。您需要为未绑定的参数使用占位符:

std::bind

N.B。此绑定表达式将复制using namespace std::placeholders; sort(copyGameEntry.begin(), copyGameEntry.end(), bind(&Lotto::compareEntry_sumPct_nonMega, game, _1, _2)); 对象,因此您应该使用game或仅使用std::ref(game),以避免不必要的副本。

或者使用lambda函数:

&game

答案 1 :(得分:2)

还有其他方法可以调用std :: sort:

#include <algorithm>
#include <vector>

struct X
{
    int value;
    bool operator < (const X& other) const { return value < other.value; }
    static bool less(const X& a, const X& b) { return a.value < b.value; }
};

struct Holder
{
    bool less(const X& a, const X& b) const { return a.value < b.value; }
};

int main ()
{
    Holder holder;
    std::vector<X> values;

    // No stateful comparison
    std::sort(values.begin(), values.end());

    // No stateful comparison
    std::sort(values.begin(), values.end(), X::less);

    // Stateful comparison
    struct Less {
        const Holder& holder;
        Less(const Holder& holder) : holder(holder) {}
        bool operator ()(const X& a, const X& b) const { return holder.less(a, b); }
    };
    std::sort(values.begin(), values.end(), Less(holder));

    // Stateful comparison
    std::sort(values.begin(), values.end(), [&holder](const X& a, const X& b) {
        return holder.less(a, b);
    });

    // Stateful comparison
    using namespace std::placeholders;
    std::sort(values.begin(), values.end(), std::bind(&Holder::less, holder, _1, _2));
}

可能在您的情况下,您缺少std :: placeholders

答案 2 :(得分:-1)

您的错误与:bind(&Lotto::compareEntry_sumPct_nonMega, game)有关。

当你调用函数std::placeholders时,你需要指定std::bind,它将被指定顺序的被调用的返回函数对象的参数替换。

您可以使用以下try - catch块进行验证:

try {
    std::sort(copyGameEntry.begin(), copyGameEntry.end(),
              bind(&Lotto::compareEntry_sumPct_nonMega, game)

} catch (std::bad_function_call& e) {
    std::cout << "ERROR: Bad function call\n";

}

在您的情况下,您需要添加:

using namespace std::placeholders;
auto func_obj = bind(&Lotto::compareEntry_sumPct_nonMega, game, _1, _2);

然后,func_obj (_1, _2)将在内部由sort()调用,如下:

func_obj(copyGameEntry[i], copyGameEntry[i+1]);

或者,您可以尝试使用以下内容:

struct Holder{
    bool less(const Entry& a, const Entry& b) const { 
        return a.sumOfNonMegaEntryPct(numbers) < b.sumOfNonMegaEntryPct(numbers); 
    }
} holder;

struct Less {
    const Holder& holder;
    Less(const Holder& holder) : holder(holder) {}
    bool operator ()(const Entry& a, const Entry& b) const { return holder.less(a, b); }
};

std::sort(copyGameEntry.begin(), copyGameEntry.end(), Less(holder));