G
Gunnar G
Hello, I just want to make sure I got it right.
I have this code
list<int> L;
L.push_back(1);
L.push_back(2);
L.push_back(3);
L.push_back(4);
list<int>::iterator iter=L.begin()+1;
L.erase(iter);
L.erase(iter);
This is a problem since I can't be sure what iter is pointing to after the
first erase, right?
So I should instead do:
iter=L.erase(iter);
iter=L.erase(iter);
With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);
since most likely the vector will not move around in the memory and the
deleted element will be replaced with the next one, so that iter will again
point to a valid element in the vector?
But I should use
iter=V.erase(iter);
iter=V.erase(iter);
here to, right?
Learning is fun.
I have this code
list<int> L;
L.push_back(1);
L.push_back(2);
L.push_back(3);
L.push_back(4);
list<int>::iterator iter=L.begin()+1;
L.erase(iter);
L.erase(iter);
This is a problem since I can't be sure what iter is pointing to after the
first erase, right?
So I should instead do:
iter=L.erase(iter);
iter=L.erase(iter);
With a vector (V), I could get away with
V.erase(iter);
V.erase(iter);
since most likely the vector will not move around in the memory and the
deleted element will be replaced with the next one, so that iter will again
point to a valid element in the vector?
But I should use
iter=V.erase(iter);
iter=V.erase(iter);
here to, right?
Learning is fun.