gpt4 book ai didi

c++ - 访问 stringstream 对象的关联字符串

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:59:22 25 4
gpt4 key购买 nike

我正在尝试访问 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 对象的关联字符串的迭代器,并且该行为将是正确的?没有对任何字符串流函数的其他调用。如何知道这是否安全?

最佳答案

仔细看看 stringstream 的 str方法。它返回字符串的拷贝,而不是对字符串对象的引用。所以理论上,只要您自己不更改字符串,您的迭代器就不会失效。

关于c++ - 访问 stringstream 对象的关联字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4728061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com