I need some clarification regarding vector and iterator. I have a vector of my_object
, and an iterator for that vector. I perform an insert at the iterator position, and a second insert at the same location:
myiteratornew = (my_vector).insert(myiterator, my_object1);
myiteratornew = (my_vector).insert(myiterator, my_object2);
After using several times this function on several kind of 'input' data, today I get a memory error. Of course I think the problem is caused by the use of the old iterator on the modified (reallocated) vector; modifying the code this way now it works:
myiteratornew = (my_vector).insert(myiterator, my_object1);
myiteratornew = (my_vector).insert(myiteratornew , my_object2);
My question is, how is it possible that I used my code several times without getting the memory error? Should the second code prevent my code causing a memory error?