c++ - boost lexical_cast <int> 检查

标签 c++ boost casting lexical-cast boost-tokenizer

这应该很简单。我有一个函数可以遍历 csv 并基于逗号进行标记化并使用标记进行操作。其中之一是将其转换为 int。不幸的是,第一个标记可能并不总是 int,所以如果不是,我想将它设置为“5”。

目前:

t_tokenizer::iterator beg = tok.begin();
if(*beg! )   // something to check if it is an int...
{
    number =5;
}
else
{
    number = boost::lexical_cast<int>( *beg );
}

最佳答案

看到 lexical_cast 抛出失败...

try {
    number = boost::lexical_cast<int>(*beg);
}
catch(boost::bad_lexical_cast&) {
    number = 5;
}

关于c++ - boost lexical_cast <int> 检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8364451/

相关文章:

c++ - boost 软件集成

go - Go 中的函数类型 - 将特定类型转换为更通用的类型

c# - C# 中的 MySQL 转换

c++ - DirectX 11 输入读取方法

c++ - 访问与使用 TraceLoggingWrite 提供的事件关联的用户数据

c++ - 如何增加 boost::variant 可以处理的类型数量

c++ - boost 多索引 : lower_bound with value_type as argument

java - java .cast 方法的不同行为

c++ - LNK 2019 和 2001 错误

c++ - 字符串流 >> 运算符