leetcode刷题中cmp用bool <可以而int -不行,有什么区别吗
//这个为啥不行啊leetcode905把偶数排前面
class Solution {
public:
static int cmp (int a, int b) {
return a%2<b%2;
}
vector<int> sortArrayByParity(vector<int>& a) {
sort (a.begin(), a.end(), cmp);
return a;
}
};
这是错误的
这是正确的
bool类型中0为false非0为true,用int转换为bool时c++中cmp的sort看0 1,c语言中cmp的qsort看正负
sort函数对比较函数的格式是有要求的