C++ 使用
我知道已经有关于它的问题了,我遵循了所有的提示,但它仍然不起作用,所以我很高兴我能理解出什么问题 我有这个结构:C++ 使用,c++,algorithm,sorting,vector,struct,C++,Algorithm,Sorting,Vector,Struct,我知道已经有关于它的问题了,我遵循了所有的提示,但它仍然不起作用,所以我很高兴我能理解出什么问题 我有这个结构: struct Scores { int _score; std::string _name; }; 我想按_分数对向量进行排序-从高到低。这是我写的: std::sort (_scores.begin(), _scores.end(), myFunction); 我有这个功能: bool myFunction (const struct Scores &i
struct Scores
{
int _score;
std::string _name;
};
我想按_分数对向量进行排序-从高到低。这是我写的:
std::sort (_scores.begin(), _scores.end(), myFunction);
我有这个功能:
bool myFunction (const struct Scores &i, const struct Scores &j)
{return i._score>j._score;}
我已经包括了算法,所以我真的不知道问题出在哪里。我发现以下错误:
error C3867: 'HighScores::myFunction': function call missing argument list;
error C2780: 'void std::sort(_RanIt,_RanIt)' : expects 2 arguments - 3 provided
谢谢在您的示例中,myFunction似乎是成员类函数。它必须是静态的,然后才能成为std::sort算法的正确谓词。或者,您可以将此函数设置为免费的优秀函数 或者你可以做一个函子
struct Sorter {
bool operator ()( const Scores &o1, const Scores &o2) {
return o1._score > o2._score;
}
};
并将其实例传递给算法:
std::sort ( _scores.begin(), _scores.end(), Sorter());
除了@bits_international answer,您还可以在C++11中这样做
std::sort(_scores.begin(), _scores.end(), [](const Score& a, const Score& b) {
return a._score > b._score;
});
希望有帮助。myFunction是成员函数吗?然后它应该是静态的。如果myFunction是成员函数,则它需要是静态的。此错误毫无意义>>“void std::sort\u RanIt,\u RanIt”:需要提供2个参数-3个。根据Alan的注释,调用成员函数与调用非成员函数在概念上是不同的。HighScores::myFunctioni,j实际上被称为HighScores::myFunction,i,j