...
Code Block |
---|
string s("rcs");
string::iterator si = s.begin();
for (size_t i=0; i < 20; ++i) {
if ( s.size() == s.capacity() ) {
break;
}
s.push_back('x');
}
s.insert(si, '*');
|
If instead of performing a push_back()
, the code were to insert into an arbitrary location in the string, all references, pointers, and iterators from the insertion point to the end of the string are invalidated.
...
The intent of these iterator invalidation rules is to give implementors greater freedom in implementation techniques. Some implementations implement method version that do not invalidate references, pointers, and iterators in all cases. Check with your implementation specific documentation and document the documentation for your implementation before attempting to access a (potentially) invalid iterator. Document any violation of the semantics specified by the standard for portability.
...