在c++ (gcc)中是否有等价的TryParse ?

Is there a TryParse equivalent in C++ (gcc)?

本文关键字:TryParse 是否 c++ gcc      更新时间:2023-10-16

在c++ (gcc)中是否有等价的TryParse ?

我想解析一个字符串,它可能包含(+31321)并将其存储为长。我知道电话号码以字符串和字符串匹配的形式存储,但出于我的需要,我希望将它们存储得尽可能长,有时它们可能包含加号(+)。如何在c++中解析它?

strtoul()和family的问题是没有真正的方法来测试失败。
如果解析失败,则返回0,而不设置errno(仅在溢出时设置)。

boost lexical cast

#include <boost/lexical_cast.hpp>

int main()
{
    try
    {
        long x = boost::lexical_cast<long>("+1234");
        std::cout << "X is " << x << "n";
    }
    catch(...)
    {
        std::cout << "Failedn";
    }
}

使用流

int main()
{
    try
    {
        std::stringstream stream("+1234");
        long x;
        char test;
        if ((!(stream >> x)) || (stream >> test))
        {
            // You should test that the stream into x worked.
            // You should also test that there is nothing left in the stream
            //    Above: if (stream >> test) is good then there was content left after the long
            //           This is an indication that the value you were parsing is not a number.
            throw std::runtime_error("Failed");
        }
        std::cout << "X is " << x << "n";
    }
    catch(...)
    {
        std::cout << "Failedn";
    }
}
使用scanf:

int main()
{
    try
    {
        char integer[] = "+1234";
        long x;
        int  len;
        if (sscanf(integer, "%ld%n", &x, &len) != 1 || (len != strlen(integer)))
        {
            // Check the scanf worked.
            // Also check the scanf() read everything from the string.
            // If there was anything left it indicates a failure.
            throw std::runtime_error("Failed");
        }
        std::cout << "X is " << x << "n";
    }
    catch(...)
    {
        std::cout << "Failedn";
    }
}

+31321可以用通常的流提取操作符解析为long

#include <iostream>
#include <sstream>
int main()
{
    std::istringstream s("+31321");
    long n;
    s >> n;
    std::cout << n << 'n';
}

演示:http://ideone.com/4rmlp

虽然解析实际的电话号码(使用括号、破折号、扩展名等)可能没有那么简单

实际上,在将字符串转换为数字之前,应该将数字"规范化"为通用格式。这需要删除所有符号,并用合适的表示方式替换它们。

但是您必须非常小心地将电话号码(不是数字:它们不受常规算术的约束)表示为字符串:以一个或多个0开头的数字与去掉0后的数字不一样:

00是+的典型替换,但是前面没有00(或+)的数字应该加00c前缀,其中c是国家代码。

在转换之前,您需要做一些预处理来获得统一的字符串表示,否则您可能会"别名"不同的东西。

输入提取操作符>>(我希望它是一个可接受的名称)应用并返回一个具有bool操作符的流&,这意味着提取已成功尝试。例如,来自Cubbi的回答:

...
    std::istringstream s("+31321");
    long n;
    if (s >> n)
      std::cout << n << 'n';
....

如果s的内容合适,这当然会成功。

有点不同(更简单,但不是类型安全),scanf家族在c++和c中也有实用的工具。你当然可以这样写这个例子:

...
    long n;
    if (sscanf("+31321", "%d", &n) == 1)
      std::cout << n << 'n';
...

正则表达式的一个子集使得这个功能相当强大:例如,用左空格修剪匹配逗号分隔的多字段:

   if (sscanf("a,b,c", " [^,], [^,], [^,]", a,b,c) == 3) ...