是否有一个STL或升压功能,以确定是否一个字符串是数字? [英] Is there a stl or boost function to determine if a string is numeric?
问题描述
我很新的C ++,提振等。
I'm very new to C++, boost etc.
我想知道如果已经有升压或STL功能我可以用它来确定一个字符串是数字。
I would like to know if there is already a function in boost or STL I can use to determine if a string is numeric.
数字字符串可能看起来像:
100
Numeric strings may look like: 100
或
100.52
我知道有吨例子怎么写这样的功能,但我想知道如果已经有一个功能,我可以用这一点。
I know there are tons of examples how to write such a function but I would like to know if there is already a function I can use for this.
我在寻找一个纯粹的C ++ - 解决方案,而不是C
I'm looking for a pure C++-solution, not C.
[更新:
我已经使用lexical_cast的转换我的琴弦,我只是想知道,如果有喜欢is_numeric我可以用这个方法...]
[UPDATE: I'm already using lexical_cast to convert my strings, I'm just wondering if there is a method like is_numeric I can use for this...]
推荐答案
没有,有没有做这个现成的,直接的方式。
No, there's not a ready-made way to do this directly.
您可以使用的boost :: lexical_cast的<双重方式>(your_string)
,如果它抛出一个异常,那么你的字符串不是双
You could use boost::lexical_cast<double>(your_string)
and if it throws an exception then your string is not a double.
bool is_a_number = false;
try
{
lexical_cast<double>(your_string);
is_a_number = true;
}
catch(bad_lexical_cast &)
{
// if it throws, it's not a number.
}
这篇关于是否有一个STL或升压功能,以确定是否一个字符串是数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!