473,396 Members | 2,068 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,396 software developers and data experts.

problem with a loop, running infinitely

nabh4u
62
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 1669
RRick
463 Expert 256MB
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 Expert Mod 8TB
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
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 Expert 256MB
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
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
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...
8
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...
9
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
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...
13
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
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
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:...
44
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
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. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
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,...
0
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...
0
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...
0
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...
0
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,...

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.