Let\'s say I have a vector(v1) of size 10. Now I want to only keep parts of the elements, using:
v1.assign(v1.begin() + 2, v1.begin() + 6);
What I\'
From the C++11 Standard:
23.3.6.2 vector constructors, copy, and assignment
template
void assign(InputIterator first, InputIterator last); 11 Effects:
erase(begin(), end()); insert(begin(), first, last);
In other words, don't use:
v1.assign(v1.begin() + 2, v1.begin() + 6);
By the time insert
is called, first
will be an invalid iterator.