471,573 Members | 1,605 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,573 software developers and data experts.

Problem with deleting vector items

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
8 1827
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
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

"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
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

"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
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
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
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.

Similar topics

3 posts views Thread by Alan | last post: by
11 posts views Thread by Yvonne | last post: by
16 posts views Thread by kaferro | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.