查找失败时增强string_algo返回值

boost string_algo return value on find failure

本文关键字:algo 返回值 string 增强 失败 查找      更新时间:2023-10-16

我想使用boost::string_algo的find first:

查找一行中的第一个空格
const boost::iterator_range<std::string::iterator> token_range = boost::find_first(line, " ");

我似乎在文档中找不到任何东西,如果它没有找到一个空格,那么它会返回什么。我是否需要测试token_range.end()与line.end()或其他什么?

谢谢!

我认为你应该测试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
}