有人知道确定字符串值是否“符合” float 的便捷方法吗?
bool IsFloat( string MyString )
{
... etc ...
return ... // true if float; false otherwise
}
最佳答案
如果你不能使用 Boost 库函数,你可以像这样编写自己的 isFloat 函数。
#include <string>
#include <sstream>
bool isFloat( string myString ) {
std::istringstream iss(myString);
float f;
iss >> noskipws >> f; // noskipws considers leading whitespace invalid
// Check the entire string was consumed and if either failbit or badbit is set
return iss.eof() && !iss.fail();
}
关于C++ IsFloat 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/447206/