C ++ std ::在类中使用谓词函数排序 [英] C++ std::sort with predicate function in Class
问题描述
我想在特定类中以某个顺序对某个结构的向量进行排序。我已经在类中定义了struct和谓词函数,并在类的方法中运行std :: sort,使用这些结构和函数。但编译错误发生。 gcc版本是4.0.1和操作系统是Mac OSX。代码如下:
类样例{
public:
struct s {
int X;
int y;
};
bool cmp(struct a,struct s b){
if(a.x == b.x)
return a.y<通过;
else
return a.x< b.x;
}
int func(void){
std :: vector< struct s> vec;
// ...
sort(vec.begin(),vec.end(),cmp); //编译错误
// ...
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 :: *) :s,sample :: s)'
...
- 定义
struct s
和函数cmp()
以外的类样本
。 - 删除
函数cmp()
并定义运算符
重载
1)
struct s {
int x;
int y;
};
bool cmp(struct a,struct s b){
if(a.x == b.x)
return a.y<通过;
else
return a.x< b.x;
}
类样例{
// ...
$ b b
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<斧头;
}
};
任何人都可以告诉这种行为的机制?为什么第一个方法调用编译错误?
谢谢。
在第一种情况下, cmp
被声明为类样本
的成员函数,因此需要这个
指针调用它。由于这个
指针不可用,编译器正在抱怨它。你可以通过声明 cmp
作为 static
函数,因为静态函数不需要这个指针调用。在第二种情况下,由于cmp被再次声明为独立函数,所以它的行为与静态函数相同。在第三种情况(具有重载运算符)中,排序算法将负责调用向量中的每个对象的函数,并且因此它编译。</ p>
I want to sort vector of certain struct with certain order in certain class. I've wrote definition of struct and predicate function in a class and run std::sort in a method of the class with these struct and function. But compilation error has occurred. gcc version is 4.0.1 and OS is Mac OSX. The code is following:
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;
}
Error message was huge and complex. So this is first two lines of it.
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)'
...
Instead of above approach, the code could run correctly with following ways.
- Define
struct s
and functioncmp()
outside ofclass sample
. - Remove
function
cmp()
and define operator overloading of<
instruct s
.
Sample code of each approach is bellow.
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;
}
};
Can anyone tell a mechanism of this behavior? Why does first approach invokes compilation error?
Thanks.
In the first case cmp
is declared as a member function of the class sample
and hence requires this
pointer for calling it. Since the this
pointer is not available compiler is complaining about it. You can make it work by declaring cmp
as static
function since static functions do not require this pointer for calling. In the second case, since cmp is declared as a stand-alone function again it will behave same as static function. In the third case (with overloaded operator), the sort algorithm will take care of calling the function for each object in the vector and hence it compiles.
这篇关于C ++ std ::在类中使用谓词函数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!