Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old September 28th, 2006, 02:05 PM
Christian Bruckhoff
Guest
 
Posts: n/a
Default 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


  #2  
Old September 28th, 2006, 02:05 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items

Christian Bruckhoff wrote:
Quote:
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.
Quote:
}
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


  #3  
Old September 28th, 2006, 08:55 PM
Roland Pibinger
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items

On Thu, 28 Sep 2006 09:20:34 -0400, "Victor Bazarov"
<v.Abazarov@comAcast.netwrote:
Quote:
>Christian Bruckhoff wrote:
Quote:
>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
  #4  
Old September 29th, 2006, 12:05 PM
Christian Bruckhoff
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items


"Victor Bazarov" <v.Abazarov@comAcast.netschrieb im Newsbeitrag
news:efgi73$cdr$1@news.datemas.de...
Quote:
Christian Bruckhoff wrote:
Quote:
>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


  #5  
Old September 29th, 2006, 12:25 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items

Christian Bruckhoff wrote:
Quote:
Quote:
>>
> 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

Quote:
>
  #6  
Old September 29th, 2006, 01:05 PM
Christian Bruckhoff
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items


"Ron Natalie" <ron@spamcop.netschrieb im Newsbeitrag
news:451D066E.9030504@spamcop.net...
Quote:
Christian Bruckhoff wrote:
>
Quote:
Quote:
>>>
>> 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


  #7  
Old September 29th, 2006, 01:15 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items

Christian Bruckhoff wrote:
Quote:
>
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;
}
}
  #8  
Old September 29th, 2006, 09:05 PM
Christian Bruckhoff
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items

Write the loop like this:
Quote:
>
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


  #9  
Old September 29th, 2006, 09:15 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Problem with deleting vector items

Christian Bruckhoff wrote:
Quote:
Quote:
>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?
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles