473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 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 1931
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: George | last post by:
VB.net 2003 standard, XP windows home edition. Installed first application OK today. When I removed the application via Control Panel, there were no problems and the app folders were deleted. ...
10
by: A_PK | last post by:
I am writing mobile application using Vb.net when i click the last row of list view, and try to delete it.... will promtpy the following message "Additional information:...
0
by: steven.shannon | last post by:
Hello, I'm writing an app that involves deleting all the items in a specified Outlook folder regardless of item type (i.e. Contacts, Tasks, etc.). This code was ported from VBA where it worked...
3
by: Alan | last post by:
I am having a strange problem with a vector. I am reading data in from a file and putting it in the vector. On the third item read in, it reaches the .push_back code. However, after that, I use...
11
by: Yvonne | last post by:
Hi, I'm running Access 2002 and have a problem deleting records on a continuous form. I thought it might be due to relationships with two other tables but having deleted these relationships,...
16
by: kaferro | last post by:
What is the typical way to loop through a vector while deleting certain elements during the loop process? The code below works, but I am wondering if there is a better solution. ...
5
krungkrung
by: krungkrung | last post by:
hi again to everyone! I made a simple program(for my VB.Net practice). The program loads an image file to a picturebox upon clicking a button. after loading the image file i have another button to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.