2

boost::string_algo の最初の検索を使用して、行の最初のスペースを検索したい:

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

ただし、スペースが見つからない場合にこれが何を返すかについて、ドキュメントには何も見つからないようです。line.end() などに対して token_range.end() をテストする必要がありますか?

ありがとう!

4

1 に答える 1

4

次のようにテストするだけでよいと思います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_rangebool 変換演算子もあるため、 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 に答える