string :: replace是否会使迭代器和引用无效? [英] Does string::replace invallidate iterators and references?
本文介绍了string :: replace是否会使迭代器和引用无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我已经搜索了迭代器并引用了类字符串的无效,但没有找到结果.
Hello I've searched for iterator and reference invalidation of class string but I didn't find a result.
我有此代码:
int main(){
std::string s = "const char* manipulation in C++";
auto beg = s.cbegin();
auto& r = *s.begin();
std::cout << s << std::endl;
std::cout << "*beg: " << *beg << std::endl;
std::cout << "r: " << r << std::endl;
s.replace(beg, beg + 11, "string");
std::cout << s << std::endl;
std::cout << "*beg: " << *beg << std::endl;
std::cout << "r: " << r << std::endl;
}
输出:
const char* manipulation in C++
*beg: c
r: c
string manipulation in C++
*beg: s
r: s
它看起来不错,但我不知道它是否是未定义的行为.谢谢!
It looks good but I don't know whether it is undefined behavior or not. Thank you!
推荐答案
根据此参考:
与此对象相关的任何迭代器,指针和引用都可以是 无效.
Any iterators, pointers and references related to this object may be invalidated.
这篇关于string :: replace是否会使迭代器和引用无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文