使用 atof 表示整数

Ric*_*ard 0 c++ atof

我继承了一些代码(从已经离开的人那里)并发现了这个小片段:

double minX = xVal.find('.') == string::npos ? (double)atoi(xVal.c_str()) : atof(xVal.c_str());
double minY = yVal.find('.') == string::npos ? (double)atoi(yVal.c_str()) : atof(yVal.c_str());
Run Code Online (Sandbox Code Playgroud)

他选择使用 atoi 来表示整数类型有什么原因吗?我看不出有什么问题:

double minX = atof(xVal.c_str());
double minY = atof(yVal.c_str());
Run Code Online (Sandbox Code Playgroud)

谢谢。

wyb*_*urn 5

实际上,您不应该使用 atoi 或 atof。它们均已弃用,已分别由 strtol 和 strtof 取代。