gpt4 book ai didi

C++ IsFloat 函数

转载 作者:IT老高 更新时间:2023-10-28 12:59:04 27 4
gpt4 key购买 nike

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

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com