我想在特定的类中按某种顺序对某个结构体的向量进行排序。我已经写了一个类的结构和谓词函数的定义,并在这个结构和函数的类的方法中运行std :: sort。但编译错误已经发生。 gcc版本是4.0.1,操作系统是Mac OSX。代码如下:C++ std :: sort类中的谓词函数
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: In member function 'int sample::func()':
sortSample.cpp:51: error: argument of type 'bool (sample::)(sample::s, sample::s)' does not match 'bool (sample::*)(sample::s, sample::s)'
...
代替上述方法,代码可以通过以下方式正确运行。
- 定义
struct s
和功能cmp()
外class sample
。 - 删除 函数
cmp()
并定义运算符<
的重载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;
}
};
谁能告诉此行为的机制?为什么第一种方法调用编译错误?
谢谢。
哇,这是一个可怕的把戏。谢谢!这是我可以让比较运算符访问类实例的成员以及要比较的两个对象的唯一方法 - 这真的有所帮助,因为在我的情况下,它允许比较对象使用相对的32位索引到一个64位指针。 – Deadcode 2010-02-13 12:55:28
请注意,调用sort()的方法会将* this的副本传递给它,而不是引用或指针,因此如果使用此技巧,请确保副本仅包含谓词所需的成员 - 最好只有一个32位或64位成员(必要时使用“this”的继承和投射)。 – Deadcode 2010-02-13 14:29:38
另请注意,* this的副本不仅被传递给sort(),而且还会继续递归传递给实用函数sort()使用,具体取决于正在使用的STL实现。 – Deadcode 2010-02-13 14:36:47