If the question can be reformulated as how to fix,
then this is something you need to do
int Size = v.size();
for (int I = 0; I < Size; I++) {
printf("*********%d\n", *v.begin());
v.erase(v.begin());
}
If you want really to know why it is not working,
this may give you a hint:
for (iter = v.begin();iter != v.end(); ++iter) {
printf("BEF *********%d\n", *iter);
iter = v.erase(iter);
printf("AFT *********%d\n", *iter);
}
Erasing any element except the last causes the vector to
be "rearanged", and return value is iterator to next element.
Erasing the last element seems not to return v.end() in my tests,
but returns the last element and this could be because the pointer
to the last location is in memory.
-haro
-----------------------------------------
Sorry if this is not complete answer, I just thought it
might help you quickly in case you are not trying to deeply
understand what is going on.
Bin Chen wrote:
Hi,
I don't know what's wrong with my program, it causes SIGSEGV:
#include <vector>
using namespace std;
int main(void)
{
vector<intv;
vector<int>::iterator iter;
int i,j,k;
i = 9;j = 10;k = 11;
v.push_back(i);
v.push_back(j);
v.push_back(k);
for (iter = v.begin();iter != v.end(); ++iter) {
printf("*********%d\n", *iter);
iter = v.erase(iter);
}
return 0;
}
Thanks.
ABAI