不建议以这种方式转换字符串:
string input = "81.312";
double val = atof(input.c_str());
答案 0 :(得分:11)
请勿在C ++中使用std::atof
。这不会检查输入错误。
使用std::stod
。这也会检查错误并相应地抛出异常。
此外,它需要std::string const &
作为参数。所以你不必通过input.c_str()
。就这样做:
double value = std::stod(input);
答案 1 :(得分:0)
这没有错,但更正确的是使用boost :: lexical_cast。
您还应该检查这些工具是否正确处理NAN和INF。