C++ 大佬帮帮我:sort自定义compare函数引发了异常

weixin_45758339 2021-04-12 06:43:40
如果sort使用myCompare程序正常运行,而使用mc会报:将一个无效参数传递给了将无效参数视为严重错误的函数异常。
这是为什么,&如何debug这种错误找到问题所在?

class Solution1 {
static bool myCompare(string x, string y) {//降序
return x + y > y + x;

}
static bool mc(string x, string y) {
return (x + y).compare(y + x);
}
string largestNumber(vector<int>& nums) {

vector<string> numStr;
string max;
for (auto iter = nums.begin(); iter != nums.end(); iter++)
numStr.push_back(to_string(*iter));
sort(numStr.begin(), numStr.end(), myCompare);//这里报错了!!!!!
for (auto a : numStr) {
max += a;
}
if (max[0] == '0')return "0";
return max;

}

};
...全文
279 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2021-04-13
  • 打赏
  • 举报
回复
引用 2 楼 flying_music 的回复:
报错可能是负数转bool有问题吧 猜的
http://cplusplus.com/reference/string/string/compare/?kw=string%3A%3Acompare http://cplusplus.com/reference/algorithm/sort/
真相重于对错 2021-04-12
  • 打赏
  • 举报
回复
sort的比较函数必须是一个函数指针 ,一个lambda表达式,一个仿函数,你那每个啥也不是,因此出错。 具体百度stl::sort 比较函数
flying_music 2021-04-12
  • 打赏
  • 举报
回复
报错可能是负数转bool有问题吧 猜的
flying_music 2021-04-12
  • 打赏
  • 举报
回复
compare不能这么用吧 只有相等的时候才是0 才是false 其他非零 都是true

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧