Problem with deleting vector items

  • Thread starter Christian Bruckhoff
  • Start date
C

Christian Bruckhoff

Hi.

I got a problem with deleting items of a vector. I did it like this:

void THIS::bashDelPerson() {
cout << "Bitte Suchstring eingeben: ";
char search[128];
cin >> search;
vector<Person>::iterator iter;
iter = persons.begin();
Person* person;

while ( iter != persons.end() ) {
if ( strstr(iter->getFirstName(), search) != 0 ) {
persons.erase(iter);
}
cout << "Moep\n";
if(iter != persons.end()){iter++;}
}
}

But this implementation only works if the Person, which should be deleted is
the last one. If it is followed by another person it looks like this, if i
print out the persons:

Thats the vector i had before:

Vorname: Hans
Nachname: Wurst

Vorname: Alf
Nachname: Egel

Vorname: John
Nachname: Doe

By deleting Alf it looks like this:

Vorname: Hans
Nachname: Wurst

Vorname: àr
Nachname: °s

How can i fix it?

Regards!
Christian
 
V

Victor Bazarov

Christian said:
I got a problem with deleting items of a vector. I did it like this:

void THIS::bashDelPerson() {
cout << "Bitte Suchstring eingeben: ";
char search[128];
cin >> search;
vector<Person>::iterator iter;
iter = persons.begin();
Person* person;

while ( iter != persons.end() ) {
if ( strstr(iter->getFirstName(), search) != 0 ) {
persons.erase(iter);

'erase' invalidates all iterators after and including the one passed
to it. You cannot use 'iter' after that. RTFM about 'erase'. It
returns an iterator. Use the return value. Most likely you need to
do

iter = persons.erase(iter);

and also fix the 'if' statement three lines down.
}
cout << "Moep\n";
if(iter != persons.end()){iter++;}
}
}
[..]

V
 
R

Roland Pibinger

'erase' invalidates all iterators after and including the one passed
to it. You cannot use 'iter' after that. RTFM about 'erase'. It
returns an iterator. Use the return value.

Usability problems are not so trivial as you seem to assume. Qt eg.
ditched STL iterators (and therfore all of STL) because of problems
like the above in favor of Java style(!) iterators:
http://doc.trolltech.com/qq/qq12-qt4-iterators.html#prosandconsofstliterators
(BTW, the link contains the answer to the OP's question).

Best regards,
Roland Pibinger
 
C

Christian Bruckhoff

Victor Bazarov said:
Christian said:
I got a problem with deleting items of a vector. I did it like this:

void THIS::bashDelPerson() {
cout << "Bitte Suchstring eingeben: ";
char search[128];
cin >> search;
vector<Person>::iterator iter;
iter = persons.begin();
Person* person;

while ( iter != persons.end() ) {
if ( strstr(iter->getFirstName(), search) != 0 ) {
persons.erase(iter);

'erase' invalidates all iterators after and including the one passed
to it. You cannot use 'iter' after that. RTFM about 'erase'. It
returns an iterator. Use the return value. Most likely you need to
do

iter = persons.erase(iter);
Hi.

Doesn't change anything. :-(

Regards!
Christian
 
R

Ron Natalie

Christian said:
Hi.

Doesn't change anything. :-(
Make sure that if you set the new iter position with the above
statement that you don't do an extra ++ operation on it which
might skip over an element (or push you past the end() location
 
C

Christian Bruckhoff

Ron Natalie said:
Make sure that if you set the new iter position with the above
statement that you don't do an extra ++ operation on it which
might skip over an element (or push you past the end() location

Hi.
I now did it like this:

if ( iter != persons.end()) {
iter = vector.ersase(iter);
}
else {
iter++;
}

So, there should be no extra increase of the iterator. But it oesnt work :-(

Regards!
Christian
 
R

Ron Natalie

Christian said:
So, there should be no extra increase of the iterator. But it oesnt work :-(

OK Here was your original code:

vector<Person>::iterator iter;
iter = persons.begin();
Person* person;

while ( iter != persons.end() ) {
if ( strstr(iter->getFirstName(), search) != 0 ) {
persons.erase(iter);
}
cout << "Moep\n";
if(iter != persons.end()){iter++;}
}

The last test above is spurious. iter is NEVER
going to be persons.end() at that point.

Write the loop like this:

vector<Person>::iterator iter = persons.begin();
while(iter != persons.end()) {
if(strstr(iter->getFirstName(), search) != 0) {
iter = persons.erase(iter);
} else {
++iter;
}
}
 
C

Christian Bruckhoff

Write the loop like this:
vector<Person>::iterator iter = persons.begin();
while(iter != persons.end()) {
if(strstr(iter->getFirstName(), search) != 0) {
iter = persons.erase(iter);
} else {
++iter;
}
}

Hi.
Did it like this, but same failure then ever. :-(

Regards!
Christian
 
R

Ron Natalie

Christian said:
Hi.
Did it like this, but same failure then ever. :-(

Regards!
Christian
Well you're going to have to give more details.
What failure. What are you expecting?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top