I have problems with the map, in the below loop.

//iterator declared as "it" and map declared as "aMap"
//key of a map declared as "key"

while(true){
    for(it = aMap.begin(); it != aMap.end(); it++){
        //some processing
        if(//some condition){
            //some processing
            aMap.erase(key);
            it--;
        }
    }

//some processing
//some conditions to break...
}

the code can be compiled and works, but skips one entry in the map... it doesnt skip the entry when i remove the "erase" and "it--" statements.

is there a way to resolve this?

any help would be much appreciated...
thanks.

Recommended Answers

All 8 Replies

I have problems with the map, in the below loop.

for(it = aMap.begin(); it != aMap.end(); it++){
    [...]
        it--;
    }

The code above looks a bit funny to me... 'it' will never reach map.end if your increase and decrease it in the same loop.

The code above looks a bit funny to me... 'it' will never reach map.end if your increase and decrease it in the same loop.

mmm... sry for giving not enough information.

i was actually checking for the values of the map with a particular key, to do some processing. it has to be in a certain order (not sorted by the key), so in an attempt to make the code more "efficient", i wanted to erase the entries that are "done" instead of checking these same entries over and over and over again.


however, in the process of doing this, one of the keys was skipped.

the original post has been edited to reflect this.

This still doesn't tell me why you increase and decrease 'it' in the same loop. You do realize that 'it' will always stay the value of map.begin right?

This still doesn't tell me why you increase and decrease 'it' in the same loop. You do realize that 'it' will always stay the value of map.begin right?

i have edited the original post.

the -- is there to prevent skipping of keys when the iterator ++ by the for loop, after erasing.

You shouldn't edit your original posts to change code. Now my reply makes no sense at all...

Anyway: Without seeing the rest of the code and some explanation about what it's supposed to do, I can't help you

You shouldn't edit your original posts to change code. Now my reply makes no sense at all...

Anyway: Without seeing the rest of the code and some explanation about what it's supposed to do, I can't help you

huh.. well, dun worry about it...

the map has string for key and int for value.

supposed to sort and output keys based on the value, cant use value as key because it can be changed, and is not unique.


the problem is that it skips the 2nd key whenever the first character (or more) of the 1st key is the same as the 2nd key.

example:

key 0: "argh" value: 6
key 1: "hello" value: 10
key 2: "hi" value: 10
key 3: "iterator" value: 678

"hello"s value satisfies the condition (value == 10), gets erased. it--, so it should be pointing at "hi" after the for loop ++. but instead of doing that, it points at "iterator", skipping "hi".

and like i said before, it does things properly when i remove the erase and -- statements... this is just an attempt to make the code a lil better...

also, please note that the example is just a small portion of the map and the results for all others are OK, as long as this first (few) character thingie doesnt occur again.

for(it = aMap.begin(); it != aMap.end(); it++){
  //some processing
  if(//some condition){
    //some processing
    aMap.erase(key);
    // this is undefined behaviour
    // the iterator is invalid after the erase
    it--;
  }
}

this will work on conforming implementations:

it = aMap.begin() ;
while(  it != aMap.end() )
{
  //some processing
  if(some_condition)
  {
    //some processing
    aMap.erase( it++ ); // postfix required
    continue; // don't increment again
  }
  ++it ; // not erased, increment
}

note: with user defined types, prefer prefix increment/decrement over postfix.

thanks for your reply vijayan121. that really helped.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.