【问题标题】:boost string_algo return value on find failure查找失败时提升 string_algo 返回值
【发布时间】:2011-08-15 12:06:14
【问题描述】:

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

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

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

谢谢!

【问题讨论】:

    标签: c++ string boost find iterator-range


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-26
      • 2023-03-21
      • 2017-08-25
      • 1970-01-01
      • 2011-12-24
      • 2017-10-30
      • 2018-12-08
      相关资源
      最近更新 更多