|
From: TSalm <TS...@fr...> - 2009-09-09 20:44:49
|
Hi,
I'm not sure it's the correct list for my question since it relates to a
C++ question... Don't hesitate to sound me off if it's not :-)
Here is my question : is there a way to delete an element of a map while
iterate on it ?
Here is my code :
/* ---------------- CODE -------------- */
#include <stdio.h>
#include <map>
#include <string>
using namespace std ;
int main( )
{
map<string,string> myMap;
myMap["1"] = "Un";
myMap["2"] = "Deux";
myMap["3"] = "Trois";
myMap["4"] = "Quatre";
for (map<string,string>::iterator it = myMap.begin();
it!=myMap.end(); ++it)
{
string key = (*it).first;
string value = (*it).second;
printf("%s - %s \n" , key.c_str() , value.c_str() );
if ( key == "2" )
{
printf("Delete key 2 !\n") ;
myMap.erase( it ) ;
}
}
return 0;
}
/* ------------------- END CODE --------------------*/
Thanks in advance,
TSalm
|