在c++ (gcc)中是否有等价的TryParse ?
Is there a TryParse equivalent in C++ (gcc)?
在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) ...
相关文章:
- 在提升multi_index容器中,是否定义了"default index"?
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 检查输入是否不是整数或数字
- 是否可以初始化不可复制类型的成员变量(或基类)
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- 是否可以通过C++扩展强制多个python进程共享同一内存
- 此代码是否违反一个定义规则
- 是否需要删除包含对象的"pair"?
- 是否可以从int转换为enum类类型
- 无论条件是否为true,if总是在c++中执行
- 如何找到大小'x'数组是否完全填充,在C++?
- 检查值是否在集合p1和p2中,但不在p3中
- 是否可以在编译时初始化数组,以便在运行时不会花费时间?
- 检查 std::shared_ptr<> 的当前底层类型是否为 T
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- GL_SHADERSTORAGE_BUFFER位置是否与其他着色器位置冲突
- 子目录是否继承属性,例如add_definitions,include_directories和父Cmakelist.t
- 标准是否使用多余的大括号(例如 T{{{10}}})定义列表初始化?
- 是否有相当于C#TryParse的boost lexical_cast
- 在c++ (gcc)中是否有等价的TryParse ?