这应该很简单。我有一个函数可以遍历 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/