stringstreamオブジェクトの関連する文字列にアクセスしてイテレータを保持しようとしていますが、これが安全かどうかを知ることができません。コードは次のとおりです:stringstreamオブジェクトの関連する文字列へのアクセス
stringstream HTMLss; // string stream of HTML document
// ... (load up HTMLss with HTML)
const string & HTMLstr = HTMLss.str(); // grab reference to associated string
string::const_iterator begin,end;
begin = HTMLstr.begin(); // grab iterators to that string as well
end = HTMLstr.end();
boost::match_results<std::string::const_iterator> matches;
// do some processing, updating the iterators along the way
while(boost::regex_search(begin, end, matches, metaexp))
{
string key(matches[1].first, matches[1].second);
string raw_val(matches[2].first, matches[2].second);
// ... some processing which does NOT manipulate HTMLss stringstream in any way
begin = matches[2].second; // updating the iterator
}
私の質問は - 上記は安全ですか? stringstreamオブジェクトの関連する文字列を指すイテレータを保持/更新でき、その動作が正しいと思われることは想定できますか?文字列ストリーム関数への他の呼び出しはありません。これが安全であるかどうかは、どのようにして知ることができますか?
イテレータを保存してから使用するまでの間にイテレータを無効にしない限り、これは安全です。つまり、実際には格納するのではなく、使用する場所(この場合はregex_search呼び出し)で直接使用することをお勧めします。 –
ちょうどスタイルの発言:カンマ(=悪いスタイル)で1行に2つの 'const_iterator'(' begin'と 'end')を宣言し、宣言のすぐ下にそれらを代入/初期化しています。より良いでしょう: 'string :: const_iterator begin = HTMLstr.begin(); string :: const_iterator end = HTMLstr.end(); '(2行で)。これはエラーが起こりにくく、コードワイズに相当します。 – rubenvb