using the following code:
list<string>::iterator iter;
for (iter=names.begin(); iter!=names.end(); iter++) {
names.erase(iter);
}
we are supposed to assume that the program compiles but does not execute properly (logical error causing a CRASH). The list holds 20 names. What is wrong, I don't see anything wrong with it (but I'm sort of new on the whole pointer thing)?
names.end() points to the imaginary space after the last element in the list. begin() points at the first element of the list.
my two guesses is that either:
iter!=names.end() should be something else, or
names.erase(iter) should be names.erase(*iter);
list<string>::iterator iter;
for (iter=names.begin(); iter!=names.end(); iter++) {
names.erase(iter);
}
we are supposed to assume that the program compiles but does not execute properly (logical error causing a CRASH). The list holds 20 names. What is wrong, I don't see anything wrong with it (but I'm sort of new on the whole pointer thing)?
names.end() points to the imaginary space after the last element in the list. begin() points at the first element of the list.
my two guesses is that either:
iter!=names.end() should be something else, or
names.erase(iter) should be names.erase(*iter);


Comment