문자열 스트림 객체의 연관된 문자열에 액세스하려고하고 반복자를 유지하려고하지만이 객체가 안전한지 여부를 알 수 없습니다. 다음은 코드입니다.문자열 스트림 객체의 연관된 문자열에 액세스
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 호출에서 사용). –
스타일 주석 : 한 줄에 두 개의'const_iterator' ('begin'과'end')를 쉼표 (bad style)로 선언하고 선언 바로 아래에 할당/초기화하고 있습니다. 더 좋을 것이다 :'string :: const_iterator begin = HTMLstr.begin(); string :: const_iterator end = HTMLstr.end();'(두 줄에). 이것은 오류가 발생하기 쉽고 코드에 상응합니다. – rubenvb