C++ ISFloat函数
有人知道确定字符串值是否“符合”浮点数的方便手段?
bool IsFloat( string MyString )
{
... etc ...
return ... // true if float; false otherwise
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
如果您无法使用Boost Library函数,则可以像这样编写自己的ISFLOAT函数。
If you can't use a Boost library function, you can write your own isFloat function like this.
您可能喜欢BOOST的LEXICAL_CAST(请参阅)。
您可以使用ISTream避免需要增压,但是坦率地说,Boost太好了,无法遗漏。
You may like Boost's lexical_cast (see http://www.boost.org/doc/libs/1_37_0/libs/conversion/lexical_cast.htm).
You can use istream to avoid needing Boost, but frankly, Boost is just too good to leave out.
灵感来自这个答案我修改了该功能以检查字符串是否为浮点数。它不需要提升&不依赖于弦乐失败 - 这只是简单的解析。
IE
被此功能正确识别为浮点。
是非val浮标的示例。如果您不想识别格式x.xxf的浮点号,只需删除条件:
从第9行中删除。
如果您不想识别没有''的数字。作为float(即不是'1',只有'1.','1.0','1.0f'...),然后您可以将最后一行更改为:
但是:有很多充分的理由使用boost的lexical_cast或使用弦线而不是这种“丑陋函数”的解决方案。但这使我可以更多地控制我想确切地识别为浮点数的格式(即小数点后的最大数字...)。
Inspired by this answer I modified the function to check if a string is a floating point number. It won't require boost & doesn't relies on stringstreams failbit - it's just plain parsing.
I.e.
is recognized by this function correctly as float.
Are examples for not-valid floats. If you don't want to recognize floating point numbers in the format X.XXf, just remove the condition:
from line 9.
And if you don't want to recognize numbers without '.' as float (i.e. not '1', only '1.', '1.0', '1.0f'...) then you can change the last line to:
However: There are plenty good reasons to use either boost's lexical_cast or the solution using stringstreams rather than this 'ugly function'. But It gives me more control over what kind of formats exactly I want to recognize as floating point numbers (i.e. maximum digits after decimal point...).
我最近写了一个功能来检查字符串是否为数字。这个数字可以是整数或浮点。
您可以扭转我的代码并添加一些单元测试。
I recently wrote a function to check whether a string is a number or not. This number can be an Integer or Float.
You can twist my code and add some unit tests.
我一直喜欢
strtof
,因为它可以指定最终指针。之所以起作用,是因为最终指针指向解析开始失败的角色,因此,如果指向NUL末端,则将整个字符串作为浮子解析。
我想,在十年中,没有人提到这种方法已经存在,我想这是因为它更像是 c风格这样做的方法。但是,它在C ++中仍然完全有效,并且比任何流解决方案都更优雅。此外,它与“+inf”“ -inf”等一起使用,依此类推,而忽略了领先的空格。
编辑
不要被空字符串所吸引,否则最终指针将处于nul终止上(因此返回true)。以上代码应为:
I always liked
strtof
since it lets you specify an end pointer.This works because the end pointer points to the character where the parse started to fail, therefore if it points to a nul-terminator, then the whole string was parsed as a float.
I'm surprised no one mentioned this method in the 10 years this question has been around, I suppose because it is more of a C-Style way of doing it. However, it is still perfectly valid in C++, and more elegant than any stream solutions. Also, it works with "+inf" "-inf" and so on, and ignores leading whitespace.
EDIT
Don't be caught out by empty strings, otherwise the end pointer will be on the nul-termination (and therefore return true). The above code should be:
[编辑:固定在禁止的初始空间和尾随的废话。]
您可以轻松地将其转换为类型
t
而不是float
。从本质上讲,这就是boost的 lexical_cast[EDIT: Fixed to forbid initial whitespace and trailing nonsense.]
You could easily turn this into a function templated on a type
T
instead offloat
. This is essentially what Boost's lexical_cast does.快速而肮脏的解决方案使用 a>:
Quick and dirty solution using
std::stof
:我想您想在输入字符串上运行正则匹配。我认为测试所有边缘案例可能相当复杂。
此站点有一些很好的信息。如果您只想跳到最后,则说:
,^[ - +]?[0-9]*\。?[0-9]+([ee] [ - +]?[0-9]+)?$
如果您理解,哪个基本上是有道理的正则语法。
I'd imagine you'd want to run a regex match on the input string. I'd think it may be fairly complicated to test all the edge cases.
This site has some good info on it. If you just want to skip to the end it says:
^[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?$
Which basically makes sense if you understand regex syntax.
使用C ++ 17:
返回两次检查都是必要的。第一个检查没有溢出或其他错误。第二个检查整个字符串是否已读取。
With C++17:
Both checks on return are necessary. The first checks that there are no overflow or other errors. The second checks that the entire string was read.
您可以使用中描述的方法在c ++?中,而不是扔
conversion_error
,返回false
(指示字符串不代表float
)和true
否则。You can use the methods described in How can I convert string to double in C++?, and instead of throwing a
conversion_error
, returnfalse
(indicating the string does not represent afloat
), andtrue
otherwise.其他响应的主要问题是性能
通常,您不需要每个角案件,例如Nan和 - /+ Inf,覆盖速度并不像具有速度那样重要。也许您不需要处理1.0E+03符号。您只需要一种快速将字符串解析为数字的方法。
这是一个简单,纯净的std :: string方法,这不是很快:
这很慢,因为它是o(k*13),检查每个字符对0 thur 9,
这是一种更快的方法:
这是一种提早终止的好处如果找到任何任何非数字字符,并且按范围进行检查,而不是每个数字数字(0-9)。因此,平均比较为每个char,o(k*3),与提前终止。
请注意,此技术与stdlib“ atof”函数中使用的技术非常相似:
The main issue with other responses is performance
Often you don't need every corner case, for example maybe nan and -/+ inf, are not as important to cover as having speed. Maybe you don't need to handle 1.0E+03 notation. You just want a fast way to parse strings to numbers.
Here is a simple, pure std::string way, that's not very fast:
This is slow because it is O(k*13), checking each char against 0 thur 9
Here is a faster way:
This has the benefit of terminating early if any non-numerical character is found, and it checks by range rather than every number digit (0-9). So the average compares is 3x per char, O(k*3), with early termination.
Notice this technique is very similar to the actual one used in the stdlib 'atof' function:
http://www.beedub.com/Sprite093/src/lib/c/stdlib/atof.c
这是一个支持科学符号的快速函数。这不是最漂亮的,但它起作用。
我(几乎)(几乎)在页面上进行了每个功能。
每个功能都通过循环运行100万次,比较了每个循环21个不同的字符串。字符串列表如下。
我还提供了一个真实表,以显示每个功能产生的内容。空白是真的,x是错误的。
我发现每个函数的性能如此不同,这很有趣。
确实要展示如何使用std ::字符串函数真的可以减慢代码的速度。而且,埃米尔(Emil)的功能还花费了17.7分钟的时间!
String List
Here is a quick function which supports scientific notation. It's not the prettiest, but it works.
I raced (almost) every function on the page.
Each function was ran through a loop 1 million times, comparing 21 different strings each loop. String list is below.
I also included a truth table to show what each function resulted with. Blank is true, x is false.
I found it pretty interesting how each function performed so differently.
Really goes to show how using the std::string functions can really slow your code down. And also how brutally slow try catch is, Emil's function took a whopping 17.7 minutes to complete!
String List
您可以使用 at at af ,然后对
0.0 0.0 0.0
,但我认为这是一个特别好的解决方案。You could use atof and then have special handling for
0.0
, but I don't think that counts as a particularly good solution.这是一个普遍的问题。查看此问题 string-> int,但方法是相同的)。
注意:要知道字符串是否可以转换,您基本上必须进行转换才能检查诸如Over/Underflow之类的内容。
This is a common question on SO. Look at this question for suggestions (that question discusses string->int, but the approaches are the same).
Note: to know if the string can be converted, you basically have to do the conversion to check for things like over/underflow.
您可以做的是使用Isringstream并根据流操作的结果返回True/False。这样的事情(警告 - 我什至还没有编译代码,这只是指南):
What you could do is use an istringstream and return true/false based on the result of the stream operation. Something like this (warning - I haven't even compiled the code, it's a guideline only):
这取决于您的信任水平,您需要以及输入数据的来源。
如果数据来自用户,则与导入的表数据相比,您必须更加谨慎,在那里您已经知道所有项目都是整数或浮点,而仅这就是您需要区分的东西。
例如,最快的版本之一就是检查“”的存在。和“ EE”。但是,然后,您可能想看看其余的都是数字。从一开始就跳过空格 - 但在中间不在中间,检查一个“”。 因此, “ EE”等。
因此,Q& d快速黑客可能会导致更复杂的正则态度(称其为称为或扫描它)。但是,您怎么知道,结果 - 虽然看起来像浮子 - 确实可以在您的机器中表示(即尝试1.2345678901234567890E1234567890)。当然,您可以在Mantissa/exponent中使用“最新”数字制作正则罚款,但这是机器/OS/编译器或有时(任何特定的)。
因此,最后,可以肯定的是,您可能必须呼吁基础系统的转换并查看您获得的内容(例外,Infinity或NAN)。
it depends on the level of trust, you need and where the input data comes from.
If the data comes from a user, you have to be more careful, as compared to imported table data, where you already know that all items are either integers or floats and only thats what you need to differentiate.
For example, one of the fastest versions, would simply check for the presence of "." and "eE" in it. But then, you may want to look if the rest is being all digits. Skip whitespace at the beginning - but not in the middle, check for a single "." "eE" etc.
Thus, the q&d fast hack will probably lead to a more sophisticated regEx-like (either call it or scan it yourself) approach. But then, how do you know, that the result - although looking like a float - can really be represented in your machine (i.e. try 1.2345678901234567890e1234567890). Of course, you can make a regEx with "up-to-N" digits in the mantissa/exponent, but thats machine/OS/compiler or whatever specific, sometimes.
So, in the end, to be sure, you probably have to call for the underlying system's conversion and see what you get (exception, infinity or NAN).
我很想忽略领先的空间,因为那是函数的
ath at也:
因此,为了匹配这一点:
I would be tempted to ignore leading whitespaces as that is what the
atof
function does also:So to match this we would:
我一直在寻找类似的东西,发现比我看到的任何内容要简单得多(尽管Floats vs. Ints仍然需要字符串打字)
或:
希望这会有所帮助!
zmazz
I was looking for something similar, found a much simpler answer than any I've seen (Although is for floats VS. ints, would still require a typecast from string)
or:
Hope this helps !
ZMazz