簡體   English   中英

查找失敗時提升 string_algo 返回值

[英]boost string_algo return value on find failure

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

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

不過,我似乎在文檔中找不到任何說明如果找不到空格會返回什么的內容。 我需要針對 line.end() 或其他東西測試 token_range.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() function 調用,只需要寫:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM