在c ++中排序类的向量 [英] Sort vector of class in c++
问题描述
我对排序函数有些麻烦...这里是我的代码:
I am having some trouble with the sort function... here is my code:
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;
}
为什么我无法编译这个没有错误?
非常感谢!
Why I can't compile this without errors? Thanks a lot!
PS:我将只显示超过五十个错误的前三行:
PS: I will show you only the first three lines of over fifty of errors:
/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]'
推荐答案
一些选项可供选择:(注意:不详尽)
Giving the OP some options to choose from: (note: not exhaustive)
class Parola {
public:
string s;
int repetition;
bool operator<(const Parola& x) const
{
return repetition < x.repetition;
}
}
使用默认的std :: less< 。
called using the default std::less<> template.
sort(p.begin(), p.end());
选项2:内部函数运算符()():
Option 2: Internal Functional operator()():
class Parola {
public:
string s;
int repetition;
bool operator()(const Parola& x, const Parola& y) const
{
return x.repetition < y.repetition;
}
}
<奇怪,但工作原理:
called with optional comparison object, as dasblinken pointed out, odd, but works:
std::sort(p.begin(), p.end(), Parola());
选项3:外部运算符<()
Option 3: External operator <()
bool operator <(const Parola& x, const Parola& y)
{
x.repetition < y.repetition;
}
这与(1)类似,使用默认的std :: less&比较器,但要求外部运算符也是Parola类的朋友,如果声明为这样,就有权访问私有数据成员。其使用与(1)相同。
This, like (1), uses the default std::less<> comparator, but requires that the external operator also be a friend of class Parola to have access to the private data members if declared as such. Its use is the same as (1).
class CompareParola
{
public:
bool operator ()(const Parola& x, const Parola& y) const
{
return x.repetition < right.repetition;
}
};
并使用者:
std::sort(p.begin(), p.end(), CompareParola());
像(3)一样,如果被访问的成员是私有的,CompareParola类必须与Parola
Like (3), the CompareParola class must be friended to Parola if the members being accessed are private:
bool ParolaLess(const Parola& x, const Parola& y)
{
return x.repetition < y.repetition;
}
类似于外部操作符或外部函数类,对象类来获得对私有成员的访问。调用像这样:
Similar to an external operator or external functional class, this also requires being friended to the object class to gain access to the private members. Invoked like such:
std::sort(p.begin(), p.end(), ParolaLess);
选项6:静态类函数
Option 6: Static Class Function
class Parola {
public:
string s;
int repetition;
static bool Less(const Parola& x, const Parola& y)
{
return x.repetition < y.repetition;
}
};
这通常未充分利用,并且具有访问所有对象成员的非常好的属性变量,包括私有变量(显然,它与类一起定义)。你可以这样做:
This is often under-utilized, and has the very nice attribute of having access to all the object member variables, including private ones (obviously, its defined with the class). You can use this by doing:
std::sort(p.begin(), p.end(), Parola::Less)
请注意,和(1)和(2)一样, 。
Note that this, like (1) and (2), keeps everything in the class.
在所有这些中,我更喜欢(1)简单,(4)独立,但每个人都有自己的口味。有时候,(5)或(6)真的很方便(我是个(6)的个人粉丝。)
Of all of these I prefer (1) for its simplicity, and (4) for its independence, but everyone has their tastes. There are times that (5) or (6) really comes in handy (and I'm a personal fan of (6)).
如果你能想到任何更多和有代表编辑它,请根据需要更新这个。请尝试让他们至少有点有用= P
If you can think of any more and have the rep to edit it, kindly update this as needed. Please do try to make them at least somewhat useful =P
这篇关于在c ++中排序类的向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!