...
Non-Compliant Example
Code Block |
---|
string s("rcs");
string::iterator si = s.begin();
// add 20 'x' chars to end of string
for (size_t i=0; i<20; ++i) {
s.push_back('x');
}
s.insert(si, '*');
|
Compliant Solution
The relationship between size and capacity makes it possible to predict when a call to a non-const member function will cause a string to perform a reallocation. This in turn makes it possible to predice when an insertion will invalidate references, pointers, and iterators (to anything other than the end of the string).
...