Spo*_*ght 2 c++ sorting class vector
我在排序功能方面遇到了一些麻烦...这是我的代码:
class Parola {
public:
string s;
int repetition;
bool operator()(const Parola *x, const Parola *y) {
return x->repetition > y->repetition;
}
};
int main(int argc, char** argv) {
...
vector<Parola> p;
...
some insertions here
...
sort(p.begin(), p.end(), Parola());
...
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为什么我不能没有错误地编译它?非常感谢!
PS:我只会告诉你超过五十个错误的前三行:
/usr/include/c++/4.2.1/bits/stl_algo.h: In function 'const _Tp& std::__median(const _Tp&, const _Tp&, const _Tp&, _Compare) [with _Tp = Parola, _Compare = Parola]':
/usr/include/c++/4.2.1/bits/stl_algo.h:2795: instantiated from 'void std::__introsort_loop(_RandomAccessIterator, _RandomAccessIterator, _Size, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Parola*, std::vector<Parola, std::allocator<Parola> > >, _Size = long int, _Compare = Parola]'
/usr/include/c++/4.2.1/bits/stl_algo.h:2866: instantiated from 'void std::sort(_RandomAccessIterator, _RandomAccessIterator, _Compare) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<Parola*, std::vector<Parola, std::allocator<Parola> > >, _Compare = Parola]'
Run Code Online (Sandbox Code Playgroud)
你的比较器需要指针,但是向量包含Parola
实例.你需要改变它.但最简单的方法是实现一个小于比较运算符.
class Parola {
public:
string s;
int repetition;
};
bool operator<(const Parola& x, const Parola& y) {
return x.repetition < y.repetition;
}
Run Code Online (Sandbox Code Playgroud)
然后你可以在sort
没有第三个参数的情况下调
sort(p.begin(), p.end());
Run Code Online (Sandbox Code Playgroud)
为OP提供一些可供选择的选项:(注意:并非详尽无遗)
class Parola {
public:
string s;
int repetition;
bool operator<(const Parola& x) const
{
return repetition < x.repetition;
}
}
Run Code Online (Sandbox Code Playgroud)
使用默认的std :: less <>模板调用.
sort(p.begin(), p.end());
Run Code Online (Sandbox Code Playgroud)
class Parola {
public:
string s;
int repetition;
bool operator()(const Parola& x, const Parola& y) const
{
return x.repetition < y.repetition;
}
}
Run Code Online (Sandbox Code Playgroud)
使用可选的比较对象调用,如dasblinken所指出的,奇怪,但有效:
std::sort(p.begin(), p.end(), Parola());
Run Code Online (Sandbox Code Playgroud)
bool operator <(const Parola& x, const Parola& y)
{
x.repetition < y.repetition;
}
Run Code Online (Sandbox Code Playgroud)
这与(1)一样,使用默认的std :: less <>比较器,但要求外部运算符也是Parola类的朋友,如果声明这样,则可以访问私有数据成员.其用途与(1)相同.
class CompareParola
{
public:
bool operator ()(const Parola& x, const Parola& y) const
{
return x.repetition < right.repetition;
}
};
Run Code Online (Sandbox Code Playgroud)
并用于:
std::sort(p.begin(), p.end(), CompareParola());
Run Code Online (Sandbox Code Playgroud)
如(3),如果被访问的成员是私有的,则必须将CompareParola类别与Parola建立联系:
bool ParolaLess(const Parola& x, const Parola& y)
{
return x.repetition < y.repetition;
}
Run Code Online (Sandbox Code Playgroud)
类似于外部运算符或外部函数类,这还需要与对象类相关联以获得对私有成员的访问.像这样调用:
std::sort(p.begin(), p.end(), ParolaLess);
Run Code Online (Sandbox Code Playgroud)
class Parola {
public:
string s;
int repetition;
static bool Less(const Parola& x, const Parola& y)
{
return x.repetition < y.repetition;
}
};
Run Code Online (Sandbox Code Playgroud)
这通常未被充分利用,并且具有访问所有对象成员变量的非常好的属性,包括私有变量(显然,它是用类定义的).你可以这样做:
std::sort(p.begin(), p.end(), Parola::Less)
Run Code Online (Sandbox Code Playgroud)
请注意,这与(1)和(2)一样,保留了类中的所有内容.
在所有这些中,我更喜欢(1)因为它的简单性,(4)因为它的独立性,但每个人都有自己的品味.有时(5)或(6)真的派上用场(我是(6)的个人粉丝).
如果你能想到更多并让代表编辑它,请根据需要更新它.请尽量使它们至少有些有用= P.