473,385 Members | 1,766 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.

Erasing Map Location Causes Error

464 Expert 256MB
Hey guys i have a map Which links vectors to each other. I have a loop and during the loop i want to delete a position in the map, but every time i do the iterator becomes null and causes an error. My code is below (well a frame of it)

MyMap <CString, int>
MyMap::iterator MapIterator;

Expand|Select|Wrap|Line Numbers
  1.  
  2.     bool TestBool = false;
  3.     for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
  4.     {
  5.         for(int j = 0; j<MyVector.size(); j++)
  6.         {
  7.            int StrCmpResult = strcmp((*MapIterator).first, MyVector[j].String); 
  8.             if(StrCmpResult == 0)
  9.             {
  10.                 TestBool = true;
  11.                break;
  12.             }
  13.         }
  14.  
  15.         if(TestBool==false)
  16.         {          
  17.                MyMap.erase(MapIterator);
  18.         }
  19.    }
  20.  
When it hits MyMap.erase(MapIterator) it does the map erase, but then when it hits the next iteration of the for loop MapIterator = null. How can i delte an item out of the map during the loop?
Sep 10 '07 #1
6 1316
Savage
1,764 Expert 1GB
Hey guys i have a map Which links vectors to each other. I have a loop and during the loop i want to delete a position in the map, but every time i do the iterator becomes null and causes an error. My code is below (well a frame of it)

MyMap <CString, int>
MyMap::iterator MapIterator;

Expand|Select|Wrap|Line Numbers
  1.  
  2.     bool TestBool = false;
  3.     for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
  4.     {
  5.         for(int j = 0; j<MyVector.size(); j++)
  6.         {
  7.            int StrCmpResult = strcmp((*MapIterator).first, MyVector[j].String); 
  8.             if(StrCmpResult == 0)
  9.             {
  10.                 TestBool = true;
  11.                break;
  12.             }
  13.         }
  14.  
  15.         if(TestBool==false)
  16.         {          
  17.                MyMap.erase(MapIterator);
  18.         }
  19.    }
  20.  
When it hits MyMap.erase(MapIterator) it does the map erase, but then when it hits the next iteration of the for loop MapIterator = null. How can i delte an item out of the map during the loop?

You need to "reset" your map iterator after you delete a value which it has pointed to.

Savage
Sep 10 '07 #2
Banfa
9,065 Expert Mod 8TB
When it hits MyMap.erase(MapIterator) it does the map erase, but then when it hits the next iteration of the for loop MapIterator = null. How can i delte an item out of the map during the loop?
The erase method returns the correct value of the iterator to continue the loop, this means you have to selectively decide how to obtain the next iterator value something like



Expand|Select|Wrap|Line Numbers
  1.  
  2.     for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); )
  3.     {
  4.         <snipped>
  5.  
  6.         if(TestBool==false)
  7.         {          
  8.                MapIterator = MyMap.erase(MapIterator);
  9.         }
  10.         else
  11.         {          
  12.                ++MapIterator;
  13.         }
  14.    }
  15.  
Sep 10 '07 #3
Studlyami
464 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1.       for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); )
  2.           {
  3.               if(TestBool==false)
  4.               {         
  5.                      MapIterator = MyMap.erase(MapIterator);
  6.               }
  7.               else
  8.               {         
  9.                      ++MapIterator;
  10.               }
  11.          }
  12.  
I tried this and it still doesn't work. It hits the string compare and i get a memory access violation.

As for trying to reset my iterator how exactly would i go about doing this? i tried the code down below, but it doesn't like MapIterator = MyMap.begin() + <int>;

Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2.       for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
  3.           {
  4.               if(TestBool==false)
  5.               {         
  6.                      MapIterator = MyMap.erase(MapIterator); 
  7.                      MapIterator = MyMap.begin() + i;
  8.               }
  9.           i++;
  10.          }
  11.  
Sep 10 '07 #4
Savage
1,764 Expert 1GB
I tried this and it still doesn't work. It hits the string compare and i get a memory access violation.

As for trying to reset my iterator how exactly would i go about doing this? i tried the code down below, but it doesn't like MapIterator = MyMap.begin() + <int>;

Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2.       for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
  3.           {
  4.               if(TestBool==false)
  5.               {         
  6.                      MapIterator = MyMap.erase(MapIterator); 
  7.                      MapIterator = MyMap.begin() + i;
  8.               }
  9.           i++;
  10.          }
  11.  
Probably because map iterator is bidirectional so it doesn't behave as a "dumb" pointer which means that it doesn't support it's arithmetic.You should be able to do this like:

Expand|Select|Wrap|Line Numbers
  1. i=0;
  2. for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
  3. {
  4.               if(TestBool==false)
  5.               {         
  6.                      MapIterator = MyMap.erase(MapIterator); 
  7.                      for(int pos=0;pos<i;pos++) ++MapIterator;
  8.  
  9.  
  10.               }
  11.               i++;
  12.  
  13.  
  14. }
Savage
Sep 10 '07 #5
Studlyami
464 Expert 256MB
Thanks, That code worked great. I needed to do pos<i-1; in the for loop and it works perfectly.
Sep 10 '07 #6
Savage
1,764 Expert 1GB
Thanks, That code worked great. I needed to do pos<i-1; in the for loop and it works perfectly.
I'm more then happy to help you.

Oh,and you wasted some valuable time(time is money) figuring out pos<i-1,please forgive me for my nonsense,I was tired.

Savage
Sep 11 '07 #7

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

Similar topics

2
by: David Smith | last post by:
I currently have Python 2.2 and 2.3.4 installed. I want to install Python 2.4, and erase 2.3.4, but retain 2.2, for I need it for my connectivity program. According to the the documentation: ...
5
by: Owen Brydon | last post by:
Hi, Is the following code legal? g++ 3.2 barfs on it, although it seems fine to me. #include <map> using std::map; int main()
3
by: Fred | last post by:
I receive this message under certain conditions: 1. "Unmanaged Code Debugging" is checked in Property Pages. 2. Certain routines are "stressed." The error does not appear unless loops are...
5
by: Alan Howard | last post by:
We're getting "ERROR (0x8007000E) Not enough storage is available to complete this operation" errors on a fairly large, busy ASP/SQL Server web site. The error is being thrown on a line calling...
11
by: eeykay | last post by:
Hello, I am facing a starnge problem while erasing the last member in a vector. I am using VC++ .NET 2002 complier. I have vector of CComPtr<..> (irrelevant here), and then I iterate over the...
5
by: ma740988 | last post by:
For starters, Happy New Year to all!! I created a vector of pairs where pair first is a primitive and pair second is a vector of ints. So now: # include <iostream> # include <vector>
3
by: =?iso-8859-1?q?Erik_Wikstr=F6m?= | last post by:
I have some code where there's this vector of pointers to objects and I need to delete and erase some of them, the problem is that to know which I need to iterate through the vector and I'm trying...
2
by: =?Utf-8?B?UmljaGFyZA==?= | last post by:
Hi, Is there any difference in handling the site navigation by using location.href or by using server.transfer? window.location.href = '<%=Response.ApplyAppPathModifier("~/MyAccount.aspx")...
4
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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,...
0
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...

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.