如果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;
}
};
uj5u.com熱心網友回復:
compare不能這么用吧 只有相等的時候才是0 才是false 其他非零 都是trueuj5u.com熱心網友回復:
報錯可能是負數轉bool有問題吧 猜的uj5u.com熱心網友回復:
sort的比較函式必須是一個函式指標 ,一個lambda運算式,一個仿函式,你那每個啥也不是,因此出錯。具體百度stl::sort 比較函式
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/275190.html
標籤:新手樂園
