2

我想首先使用 boost::string_algo 的 find 找到一行中的第一个空格:

const boost::iterator_range<std::string::iterator> token_range = boost::find_first(line, " ");

不过,我似乎在文档中找不到任何说明如果找不到空格会返回什么的内容。我需要针对 line.end() 或其他东西测试 token_range.end() 吗?

谢谢!

4

1 回答 1

4

我认为你应该像这样 test token_range.empty()

const boost::iterator_range<std::string::iterator> token_range = boost::find_first(line, " ");
if (!token_range.empty())
{
    // Found a a match
}

boost::iterator_range还有一个 bool 转换运算符,因此您甚至可以删除 empty() 函数调用,只需编写:

const boost::iterator_range<std::string::iterator> token_range = boost::find_first(line, " ");
if (token_range)
{
    // Found a a match
}
于 2011-04-29T13:14:12.267 回答