我不熟悉 C++ 以及编写和调试语言所需的内存细微差别。谁能告诉我为什么下面的代码给我一个分段错误?
string Polynomial::toString(){
int i, exponent;
stringstream result;
for (i = 0; i < coeffs.size(); i++){
// For first non-zero coefficient
if (result.str().empty()){
if(coeffs[i] < 0)
result << "-";
if(coeffs[i] != 0)
result << coeffs[i];
}
else{
if(coeffs[i] < 0)
result << " - " << abs(coeffs[i]);
else if(coeffs[i] > 0)
result << " + " << coeffs[i];
}
exponent = (coeffs.size() - i - 1);
if (coeffs[i] != 0){
if (exponent > 1)
result << coeffs[i] << "x^" << exponent;
else if(exponent == 1)
result << coeffs[i] << "x";
}
}
result.str();
}