By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,741 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

Problem with deleting vector items

P: n/a
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
Sep 28 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Christian Bruckhoff wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Sep 28 '06 #2

P: n/a
On Thu, 28 Sep 2006 09:20:34 -0400, "Victor Bazarov"
<v.********@comAcast.netwrote:
>Christian Bruckhoff wrote:
>I got a problem with deleting items of a vector. I did it like this:

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.
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...ofstliterators
(BTW, the link contains the answer to the OP's question).

Best regards,
Roland Pibinger
Sep 28 '06 #3

P: n/a

"Victor Bazarov" <v.********@comAcast.netschrieb im Newsbeitrag
news:ef**********@news.datemas.de...
Christian Bruckhoff wrote:
>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
Sep 29 '06 #4

P: n/a
Christian Bruckhoff wrote:
>>
iter = persons.erase(iter);
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

>
Sep 29 '06 #5

P: n/a

"Ron Natalie" <ro*@spamcop.netschrieb im Newsbeitrag
news:45**************@spamcop.net...
Christian Bruckhoff wrote:
>>>
iter = persons.erase(iter);
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
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
Sep 29 '06 #6

P: n/a
Christian Bruckhoff wrote:
>
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;
}
}
Sep 29 '06 #7

P: n/a
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
Sep 29 '06 #8

P: n/a
Christian Bruckhoff wrote:
>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

Well you're going to have to give more details.
What failure. What are you expecting?
Sep 29 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.