[英]C++ std::sort with predicate function in Class
我想在某个类中按某种顺序对某个结构的向量进行排序。 我在类中编写了struct和predicate函数的定义,并在具有这些struct和function的类的方法中运行std :: sort。 但是发生了编译错误。 gcc版本是4.0.1,OS是Mac OSX。 代码如下:
class sample {
public:
struct s {
int x;
int y;
};
bool cmp (struct s a, struct s b) {
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
int func(void) {
std::vector <struct s> vec;
// ...
sort(vec.begin(), vec.end(), cmp); // compilation error
// ...
return 0;
}
};
int main(void) {
sample *smp = new sample();
smp->func();
return 0;
}
错误消息庞大而复杂。 所以这是前两行。
sortSample.cpp:在成员函数'int sample :: func()'中:
sortSample.cpp:51:error:类型'bool(sample ::)(sample :: s,sample :: s)'的参数与'bool(sample :: *)不匹配(sample :: s,sample :: S)”
...
代替上述方法,代码可以通过以下方式正确运行。
class sample
之外定义struct s
和function cmp()
。 cmp()
并定义<
in struct s
运算符重载。 每种方法的示例代码如下。
1)
struct s {
int x;
int y;
};
bool cmp (struct s a, struct s b) {
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
class sample {
// ...
2)
struct s {
int x;
int y;
bool operator<(const struct s & a) const {
if (x == a.x)
return y < a.y;
else
return x < a.x;
}
};
任何人都可以告诉这种行为的机制吗? 为什么第一种方法会调用编译错误?
谢谢。
在第一种情况下, cmp
被声明为class sample
的成员函数,因此需要this
指针来调用它。 由于this
指针不可用,编译器正在抱怨它。 您可以通过将cmp
声明为static
函数来使其工作,因为静态函数不需要此指针进行调用。 在第二种情况下,由于cmp再次被声明为独立函数,因此它的行为与静态函数相同。 在第三种情况下(使用重载运算符),排序算法将负责为向量中的每个对象调用函数,从而进行编译。
由于cmp
与任何特定的样本实例无关,因此将其设为静态成员函数。
可以在您的可能性中列出的第三种方法是使用operator():
bool operator() (const s& a, const s& b) const
{
if (a.x == b.x)
return a.y < b.y;
else
return a.x < b.x;
}
sort(vec.begin(), vec.end(), *this);
我认为在类之外定义cmp
是最好的,因为当你需要它来访问类中的一些私有特性时,你应该只将一个函数作为一个成员函数,从逻辑上讲,它在那里是合适的。 cmp
只是一个实用程序函数,它为您的类sample
提供了实现的功能,但实际上并不需要访问私有成员。 此外,它可能不会在对象的上下文中调用(它的唯一操作在其参数上工作;在this
指针上没有任何内容),也不能在类sample::cmp
的上下文中调用它。 虽然看起来似乎是一个微不足道的观点,但通常给予函数或代码不必要的访问宽度可能是许多软件错误或设计卷积的来源的开始。
执行上述操作的额外好处是,您对std::sort
调用将起作用,这可以回答您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.