473,785 Members | 2,498 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with deleting vector items

Hi.

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

void THIS::bashDelPe rson() {
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(i ter);
}
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 1953
Christian Bruckhoff wrote:
I got a problem with deleting items of a vector. I did it like this:

void THIS::bashDelPe rson() {
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(i ter);
'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(i ter);

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.********@com Acast.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(i ter);

'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.********@com Acast.netschrie b 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::bashDelPe rson() {
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(i ter);

'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(i ter);
Hi.

Doesn't change anything. :-(

Regards!
Christian
Sep 29 '06 #4
Christian Bruckhoff wrote:
>>
iter = persons.erase(i ter);
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.ne tschrieb im Newsbeitrag
news:45******** ******@spamcop. net...
Christian Bruckhoff wrote:
>>>
iter = persons.erase(i ter);
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(i ter);
}
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(i ter);
}
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(i ter);
} 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(i ter);
} 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(it er->getFirstName() , search) != 0) {
iter = persons.erase(i ter);
} 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
2692
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. But when I installed the app from a CD-ROM (Release folder is in D:), when I remove the app in Control Panel these problems occur: Control Panel does not delete the application folders. When I try to delete them I get message "Cannot delete file:...
10
4404
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: ArgumentOutOfRangeException" what does that mean ? I can delete any row without problem but except last row only....
0
1538
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 okay, but now in .NET it is exceedingly too slow when attempting to delete large collections (roughly 10,000). It is on the order of 10 items deleted every 30 seconds. My function is as follows: Private Sub DeleteAllEntries()
3
1278
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 .size(), and it says that it only has two items. This is bizarre. Any thoughts? Code fragment may be found below. Thanks, Alan while (inFile >sensed_data.time >sensed_data.sensor_who >> sensed_data.aircraft_who
11
1858
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, I'm still not able to delete a record. The code I am using is as follows:-
16
2521
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. vector<intvTmp; vTmp.push_back(1); vTmp.push_back(2); vTmp.push_back(1); vTmp.push_back(2);
5
23921
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 delete the loaded image file from the directory. But I have a problem deleting the image file used by the picturebox even if I set the "PictureBox1.Image = Nothing" before deletion takes place. The error says, "The process cannot access the file...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10341
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9954
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.