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; -
-
bool TestBool = false;
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
-
{
-
for(int j = 0; j<MyVector.size(); j++)
-
{
-
int StrCmpResult = strcmp((*MapIterator).first, MyVector[j].String);
-
if(StrCmpResult == 0)
-
{
-
TestBool = true;
-
break;
-
}
-
}
-
-
if(TestBool==false)
-
{
-
MyMap.erase(MapIterator);
-
}
-
}
-
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?
6 1316
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; -
-
bool TestBool = false;
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
-
{
-
for(int j = 0; j<MyVector.size(); j++)
-
{
-
int StrCmpResult = strcmp((*MapIterator).first, MyVector[j].String);
-
if(StrCmpResult == 0)
-
{
-
TestBool = true;
-
break;
-
}
-
}
-
-
if(TestBool==false)
-
{
-
MyMap.erase(MapIterator);
-
}
-
}
-
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
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 -
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); )
-
{
-
<snipped>
-
-
if(TestBool==false)
-
{
-
MapIterator = MyMap.erase(MapIterator);
-
}
-
else
-
{
-
++MapIterator;
-
}
-
}
-
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); )
-
{
-
if(TestBool==false)
-
{
-
MapIterator = MyMap.erase(MapIterator);
-
}
-
else
-
{
-
++MapIterator;
-
}
-
}
-
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>; -
int i = 0;
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
-
{
-
if(TestBool==false)
-
{
-
MapIterator = MyMap.erase(MapIterator);
-
MapIterator = MyMap.begin() + i;
-
}
-
i++;
-
}
-
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>; -
int i = 0;
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
-
{
-
if(TestBool==false)
-
{
-
MapIterator = MyMap.erase(MapIterator);
-
MapIterator = MyMap.begin() + i;
-
}
-
i++;
-
}
-
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: - i=0;
-
for(MapIterator = MyMap.begin(); MapIterator != MyMap.end(); ++MapIterator)
-
{
-
if(TestBool==false)
-
{
-
MapIterator = MyMap.erase(MapIterator);
-
for(int pos=0;pos<i;pos++) ++MapIterator;
-
-
-
}
-
i++;
-
-
-
}
Savage
Thanks, That code worked great. I needed to do pos<i-1; in the for loop and it works perfectly.
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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:
...
|
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()
|
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...
|
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...
|
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...
|
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>
|
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...
|
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")...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |