I would like to know is it possible to sort number by even or odd using the std::sort function.
I have the following codes but i am not sure how to implement in the std::sort
inline bool isEven(const Point n) {
return n.getX()%2==0;
}
Is this correct
vector<Point> c;
std::sort(c.begin(),c.end(),isEven)
Please advice.
From what I understand of your question, you want to separate
oddandevennumbers. If that’s the case,std::partitionwill do just that.If you want to sort by ascending values AND separate
oddandevennumbers, I would use something similar to this piece of code (still, you will have to figure out which component of yourPointyou want to sort on)This function can be used with
std::sort, here’s a short example:Will give you the following output:
For other types simply change the
intor make it atemplateparameter