C ++向量排序奇数,甚至留下 [英] C++ Vector sort odd and even stay
问题描述
此代码有什么问题?我想对奇数进行排序,但还要保留它们的位置,为此设置了函数,但编译器无法通过
whats wrong about this code? I want to sort odd numbers but even stay int their places, made function for this but compilator wont pass this
有错误:
错误:必须调用对非静态成员函数的引用
error: reference to non-static member function must be called
有一个代码:
public:
std::vector<int> sortArray(std::vector<int> array)
{
std::vector<int> sortedArray(array);
std::sort ( sortedArray.begin() , sortedArray.end() , oddSort );
}
bool oddSort ( const int& left , const int& right ){
if ( left % 2 && right % 2 )
return left < right;
else if ( left % 2 )
return false;
else if ( right % 2 )
return true;
return left < right;
}
推荐答案
Danh的评论是正确的,但这实际上是一个重复的问题.但是,目前尚无重复的答案.
Danh's comment is right, but this is actually a duplicate question. However, there are no duplicates with accepted answers yet.
所以:问题是成员函数需要一个this
指针,除非它们是static
.而且std::sort
不会为您提供this
指针,也不需要一个.因此,将其设置为static
是直接的解决方案.
So: the problem is that member functions need a this
pointer, unless they're static
. And std::sort
won't give you a this
pointer, nor do you need one. So making it static
is the straightforward solution.
这篇关于C ++向量排序奇数,甚至留下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!