Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
是否有stl或boost函数来确定字符串是否为数字? 我对C++、Boost等< /P>非常陌生。_C++_Boost_Stl - Fatal编程技术网

是否有stl或boost函数来确定字符串是否为数字? 我对C++、Boost等< /P>非常陌生。

是否有stl或boost函数来确定字符串是否为数字? 我对C++、Boost等< /P>非常陌生。,c++,boost,stl,C++,Boost,Stl,我想知道boost或STL中是否已经有一个函数可以用来确定字符串是否为数字 数字字符串可能如下所示: 一百 或 100.52 我知道有很多关于如何编写这样一个函数的例子,但我想知道是否已经有一个函数可以用于此 我正在寻找一个纯C++的解决方案,而不是C [更新: 我已经在使用词法转换来转换我的字符串,我只是想知道是否有一种像is\u numeric这样的方法可以用于此…]您可以在字符串上尝试一种方法。不,没有现成的方法可以直接进行转换 您可以使用boost::lexical\u cast(您的\

我想知道boost或STL中是否已经有一个函数可以用来确定字符串是否为数字

数字字符串可能如下所示: 一百

100.52

我知道有很多关于如何编写这样一个函数的例子,但我想知道是否已经有一个函数可以用于此

我正在寻找一个纯C++的解决方案,而不是C

[更新:
我已经在使用词法转换来转换我的字符串,我只是想知道是否有一种像is\u numeric这样的方法可以用于此…]

您可以在字符串上尝试一种方法。

不,没有现成的方法可以直接进行转换

您可以使用
boost::lexical\u cast(您的\u字符串)
std::stod(您的\u字符串)
,如果它引发异常,则您的字符串不是双精度字符串

C++11:

    bool is_a_number = false;
    try
    {
        std::stod(your_string);
        is_a_number = true;
    }
    catch(const std::exception &)
    {
        // if it throws, it's not a number.
    }
促进:

    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.
    }
bool是一个数字=false;
尝试
{
词法转换(你的字符串);
是一个数字=真;
}
捕捉(错误的词汇投射&)
{
//如果它抛出,它不是一个数字。
}

如果转换“吃掉”了原始字符串中的所有字符(
eof()
),则使用
stringstream
并返回
true

bool是数字的(const std::string&str){
std::stringstream conv;
双tmp;
conv>tmp;
return conv.eof();
}
可以使用
boost::regex
(或者
std::regex
,如果您有C++0x的话); 您可以定义要接受的内容(例如,在您的上下文中, “0x12E”是否为数字?)。对于C++整数:

"\\s*[+-]?([1-9][0-9]*|0[0-7]*|0[xX][0-9a-fA-F]+)"
< C++浮点:

"\\s*[+-]?([0-9]+\\.[0-9]*([Ee][+-]?[0-9]+)?|\\.[0-9]+([Ee][+-]?[0-9]+)?|[0-9]+[Ee][+-]?[0-9]+)"
但取决于你在做什么,你可能不需要这样做 支持那些复杂的事情。你举的两个例子是 覆盖

"[0-9]+(\\.[0-9]*)?"
比如说

如果以后需要该数值,它也可能是 同样容易将字符串转换为
istringstream
,以及 立即进行转换。如果没有错误,你呢 提取所有字符,字符串是一个数字;如果没有,, 事实并非如此。这将减少您对确切时间的控制
但是,您要接受的格式。

如果性能是一个问题,我会使用。。而不是
std::stringstream

#include <string>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi_numeric.hpp>

bool is_numeric(std::string const& str)
{
    std::string::const_iterator first(str.begin()), last(str.end());
    return boost::spirit::qi::parse(first, last, boost::spirit::double_)
        && first == last;
}
#包括
#包括
#包括
布尔是数字的(std::string const&str)
{
std::string::const_迭代器first(str.begin()),last(str.end());
返回boost::spirit::qi::parse(第一,最后,boost::spirit::double)
&&第一个=最后一个;
}
如果要允许尾随空格,请执行以下操作:

#include <string>
#include <boost/spirit/include/qi_parse.hpp>
#include <boost/spirit/include/qi_numeric.hpp>
#include <boost/spirit/include/qi_char_class.hpp>
#include <boost/spirit/include/qi_operator.hpp>

bool is_numeric(std::string const& str)
{
    std::string::const_iterator first(str.begin()), last(str.end());
    return boost::spirit::qi::parse(first, last,
            boost::spirit::double_ >> *boost::spirit::qi::space)
        && first == last;
}
#包括
#包括
#包括
#包括
#包括
布尔是数字的(std::string const&str)
{
std::string::const_迭代器first(str.begin()),last(str.end());
返回boost::spirit::qi::parse(第一,最后,
boost::spirit::double\u>>*boost::spirit::qi::space)
&&第一个=最后一个;
}
以下代码

如果“str”仅由0~9组成,则返回true,否则返回false

return str.find_first_not_of("0123456789") == std::string::npos

不需要增压,只需要stl。。。
可以将单个字符检查为int(c>='0'&&c='0'&&c从c++11中,您可以简单地分别使用
std::stof
std::stod
std::stold
中的一种来转换为
float
double
long double
。如果出现问题,它们会将字符串转换为数值或引发异常(请参阅)。下面是一个使用
std::stod
的示例:

bool isNumeric(const std::string& str) {
  try {
    std::stod(str); // can safely ignore the return value, function is not [[nodiscard]]
    return true;
  }
  catch (const std::exception&) {
    return false;
  }
}

还查看了字符串转换为整数的函数。

只是想知道,C解决方案有什么问题?相关:@ Mehrdad:我不喜欢把C和C++混合。只是个人观点。谢谢你投票给我的评论,无论谁做……)都在检查EF()。真的要做正确的事情吗?@Stefan:是。@Helltone:这可以用一两行代码来完成:
(std::istringstream(str)>>num.ws().eof()
。注意调用
ws
来去掉尾随空格,这是可选的。(但是,此快捷方式会丢失空字符串或所有空白字符串。同样,更完整的实现应该返回
conv&&conv.eof()
)@Stefan在这种情况下,它可能是合适的;我会使用
return conv.get()==EOF;
但区别可能在于风格,而不是其他任何东西。这实际上就是词法强制转换所做的。@Alexandr C。除了
词法强制转换
通过异常报告错误之外;如果我理解这个问题,这不是一个例外情况。添加必要的try/catch代码最终需要与doi一样多的行如果你不知道<代码> IoStult这是正确的,并且从其他问题和答案中判断,很多人都不知道,那么这可能是一个重要的考虑点。这是一个快速的解决办法。(副本+字符串流+例外),这类东西经常在循环中使用。此外,异常法西斯分子会告诉你,你在流控制中使用异常,而不是在异常情况下使用异常。@Alexandre当然,非常值得指出,但OP没有列出任何效率约束。嗨Alexandre,嗨Matt,我只是想知道我是否必须发明轮子:).但是,尽管如此,你的建议还是非常受欢迎的,Alexandre!@Inno-Sure!看起来厨房的水槽确实隐藏在标准图书馆和boost里面,但是当你看水槽下面时,有些管道没有连接:-)这是正确的,但可能是过时的/过多的,因为C++ 11。参见现代C++。谢谢我的注意。我会记住的例子为Booo::ListalyCost(因此STD::StrugStand)可能有点重。@arr_sea:请记住,现代推荐的是Spirit.X3而不是Spirit.Qi,除非你坚持使用C++03.:-]Ple
return str.find_first_not_of("0123456789") == std::string::npos
#include <string>
#include <algorithm>
bool isNumeric(std::string strValue) 
{
    if (strValue.empty())
        return false;
    else
        return (std::find_if_not( std::begin(strValue)
                            , std::end(strValue)
                            , [](char c)
                              { return (c >= '0' && c <= '9'); }
                            ) == std::end(strValue)
                );
}
bool isNumeric(const std::string& str) {
  try {
    std::stod(str); // can safely ignore the return value, function is not [[nodiscard]]
    return true;
  }
  catch (const std::exception&) {
    return false;
  }
}