|
From: TSalm <TS...@fr...> - 2009-09-09 21:29:40
|
Thanks, this work perfectly !
And thanks too for your advice about comp.lang.c++.
> TSalm,
>
> This is a much general C++ question rather than that about MinGW. I
> think the newsgroup comp.lang.c++ would be better.
>
> It is not safe to delete an element of a map before using the iterator
> pointing to it. One of my methods may like this:
>
> for (map<string, string> it = myMap.begin(); it != myMap.end(); )
> {
> if (it->first == "2") // condition for deleting
> {
> map<string, string> toDel = it;
> it++;
> myMap.erase(toDel);
> }
> else
> {
> it++;
> }
> }
>
> On Thu, Sep 10, 2009 at 4:44 AM, TSalm<TS...@fr...> wrote:
>
>> 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 --------------------*/
>>
>>
|