C++ IsFloat 函数

标签 c++ string floating-point

有人知道确定字符串值是否“符合” 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/

相关文章:

C++:在这种情况下如何最优雅地最小化实例复制?

将 float 转换为 char*

Java字符串替换和NUL(NULL,ASCII 0)字符?

c - 从 argv[] 获取特定的字符值并创建一个新的 const char

javascript - 如何在字符串上实现 "EndsWith"?

java - 为什么 Math.round() 为 NaN 参数返回 0?

c++ - std::remquo 目的和用法?

c++ - 为什么 cin 在使用 getline 后没有获取缓冲区数据?

c++ - 如何避免一直写 std::reference_wrapper 版本模板?

c++ - gdb 无法进入 printf