tags:

views:

81

answers:

1

Hi All,

I'm having a problem with stl map. Initially I fill the map with data like so.

//loop
pair< int, int > xy (x,y);
currentMap.insert( make_pair(xy), value); //map< pair<int, int>, bool>
prevMap.insert( make_pair(xy), value);
// End Loop

Then I delete an element according to some rules like so.

currentMap.erase( make_pair(xy) );

I later do a swap of the two map objects.

prevMap = currentMap;

After the swap all of the elements are assigned to true. If I search for element xy... *edit - i got a ahead of myself. Sorry.

i = currentMap.find( make_pair(xy) );
return i->second; // Always true after swap.

truth will always evaluate to true. Will initializing a map set the bool value to true? Can I initialise with all bools at false.

Thanks.

+1  A: 

You can't initialize the map's values to anything. You can only insert values along with the key. Your last code snippet should look like this if you want to return false when the key is missing:

i = currentMap.find( make_pair(xy) );
if (i != currentMap.end())
    return i->second;
return false;
chrisaycock
Woops. I made a mistake in my post. Thanks for catching it chrisaycock :S
sdk900
I just tried this based on your post before the edit. Thanks for the tip, but alas I still have problem after the swap. I believe it may be else where in my code...
sdk900
Actually chrisaycock this did solve my problem. Do you know why I was voted down? I realise my question originally had mistakes... Anyway Thanks heaps. I'm heading to bed.
sdk900