473,549 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

problem with a loop, running infinitely

nabh4u
62 New Member
hi,
i have a program where every thing is working properly. i have a vector with some values. i use iterators and delete a specific value in the vector. here the loop runs infinitely only for some values. i dont know what is the problem.
sample code:

Expand|Select|Wrap|Line Numbers
  1. vector<int>::iterator Iterator;                    
  2. for( Iterator = vect[temp3].cmatch.begin();     
  3.         Iterator != vect[temp3].cmatch.end();Iterator++ )
  4. {
  5.     if(*Iterator==temp2)
  6.     vect[temp3].cmatch.erase(Iterator);
  7. }
here temp 2 and temp 3 are some temporary variables where i store some data temporaryily for each cycle. vect is a vector of the structure type i use and cmatch is just a vector of integer type.
i have some values in the cmatch which i compare with the value i want to delete and if equals i delete that value from the cmatch.
when i run this program and give all the input values it crashes and when i tried to debug it i found that this loop was going into infinite state.
i dont know what is the problem with this...
please help me as i have to submit my program within 2 days..
thank you in advance..
Feb 16 '07 #1
5 1674
RRick
463 Recognized Expert Contributor
You have to be careful with erasing iterators. It is possible that the iterator is not valid after an erase.

To get around this problem, erase does return the value of the next valid iterator. Try this in your loop:

Expand|Select|Wrap|Line Numbers
  1. {
  2.     if(*Iterator==temp2)
  3.     Iterator = vect[temp3].cmatch.erase(Iterator);
  4. }
  5.  
Feb 16 '07 #2
Banfa
9,065 Recognized Expert Moderator Expert
Expand|Select|Wrap|Line Numbers
  1. {
  2.     if(*Iterator==temp2)
  3.     Iterator = vect[temp3].cmatch.erase(Iterator);
  4. }
  5.  
Unfortunately this solution will cause the loop to not check values appearing after a value that is erased because of the ++ in the loop moving the iterator on 1 more. You need

Expand|Select|Wrap|Line Numbers
  1. vector<int>::iterator Iterator;                    
  2. for( Iterator = vect[temp3].cmatch.begin();     
  3.         Iterator != vect[temp3].cmatch.end(); )
  4. {
  5.     if(*Iterator==temp2)
  6.     {
  7.         Iterator = vect[temp3].cmatch.erase(Iterator);
  8.     }
  9.     else
  10.     {
  11.         Iterator++;
  12.     }
  13. }
Feb 16 '07 #3
nabh4u
62 New Member
Unfortunately this solution will cause the loop to not check values appearing after a value that is erased because of the ++ in the loop moving the iterator on 1 more. You need

Expand|Select|Wrap|Line Numbers
  1. vector<int>::iterator Iterator;                    
  2. for( Iterator = vect[temp3].cmatch.begin();     
  3.         Iterator != vect[temp3].cmatch.end(); )
  4. {
  5.     if(*Iterator==temp2)
  6.     {
  7.         Iterator = vect[temp3].cmatch.erase(Iterator);
  8.     }
  9.     else
  10.     {
  11.         Iterator++;
  12.     }
  13. }
hey first of all thank you for the suggestion..
i tried it..it works like the loop doesnt go into an infinite loop but when i display the values of the vector it does not delete the particular value...
for example: if i have only 2 values in d vector like 0 & 2 and when i do that loop 2 is equal and it should be deleted from the vector...bt it is not getting deleted. it is displayed in the output.
the output should be
0
but it shows
0 2
what should i do now??
Feb 18 '07 #4
RRick
463 Recognized Expert Contributor
Are you sure the erase method was called? The erase techinque in a loop is suppose to work. Try adding a cout after the erase to make sure.

There is another way to remove items from a vector and that is to use the erase-remove idiom (from "Effective STL" by Scott Meyers). This works well with vectors of ints and doubles. Try the following (I'm using Vector vect<int> as an example):
Expand|Select|Wrap|Line Numbers
  1. vect.erase( remove( vect.begin(), vect.enc(), 99));
  2.  
Feb 18 '07 #5
nabh4u
62 New Member
Are you sure the erase method was called? The erase techinque in a loop is suppose to work. Try adding a cout after the erase to make sure.

There is another way to remove items from a vector and that is to use the erase-remove idiom (from "Effective STL" by Scott Meyers). This works well with vectors of ints and doubles. Try the following (I'm using Vector vect<int> as an example):
Expand|Select|Wrap|Line Numbers
  1. vect.erase( remove( vect.begin(), vect.enc(), 99));
  2.  
thank you very very much...but i got the solution for my problem.
this works:

Expand|Select|Wrap|Line Numbers
  1. vector<int>::iterator Iterator;                    
  2. for( Iterator = vect[temp3].cmatch.begin();     
  3.         Iterator != vect[temp3].cmatch.end(); )
  4. {
  5.     if(*Iterator==temp2)
  6.     {
  7.         Iterator = vect[temp3].cmatch.erase(Iterator);
  8.     }
  9.     else
  10.     {
  11.         Iterator++;
  12.     }
  13. }
Feb 18 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

36
42369
by: Remi Villatel | last post by:
Hi there, There is always a "nice" way to do things in Python but this time I can't find one. What I'm trying to achieve is a conditionnal loop of which the condition test would be done at the end so the loop is executed at least once. It's some way the opposite of "while". So far, all I got is:
8
6435
by: koorb | last post by:
I am starting a program from a module with the Sub main procedure and I want it to display two forms for the program's interface, but when I run the program both forms just open and then program closes. Dim FORM1 As New Form1 Dim FORM2 As New form2 Sub main() FORM1.Show() FORM2.Show()
9
18982
by: Bill Nguyen | last post by:
I need a VB routine to loop thru a select top folder to find all subfolders and list all subfolders/files under each of these subfolders. Any help is greatly appreciated. Bill
102
4475
by: tom fredriksen | last post by:
Hi I was doing a simple test of the speed of a "maths" operation and when I tested it I found that removing the loop that initialises the data array for the operation caused the whole program to spend twice the time to complete. If the loop is included it takes about 7.48 seconds to complete, but when removed it takes about 11.48 seconds. ...
13
5254
by: Scamjunk | last post by:
I have the following code for a generic stack implementation. ---------------- void Push(void *value, void *Stack, int *top) { if(!(IsFull(Stack, top))) { (*top)++; Stack = *value; }
1
1624
by: will | last post by:
All Hope you can help with the following.. I am using recursion to calculate the sum of DEBT_INPUT_VALUE_BEFORE_SPLIT * CTXR_TAX_RATE elements, along the lines of sum(a*b) ie
29
5065
by: garyusenet | last post by:
I'm trying to investigate the maximum size of different variable types. I'm using INT as my starting variable for exploration. I know that the maximum number that the int variable can take is: 65,535. But i'm trying to write a program to test this, assuming I didn't know this number in advance. I came up with the following but have two...
44
4320
by: James Watt | last post by:
can anyone tell me how to do an infinite loop in C/C++, please ? this is not a homework question .
9
2137
by: Salad | last post by:
I have access, for testing at my client's site, a Win2000 computer running A2003 retail. He recently upgraded all of his other machines to DualCore Pentiums with 2 gig ram and run A2003 runtime. I believe all current SPs for Windows and Office are installed on the fast machines. I have 1 process/subroutine that has worked for a couple of...
0
7471
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...
0
7740
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. ...
1
7503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
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...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3517
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1962
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
0
784
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.