Delete an element in a map while iterate on it

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Delete an element in a map while iterate on it

TSalm
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


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete an element in a map while iterate on it

Linlin Yan
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<[hidden email]> 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 --------------------*/
>
> Thanks in advance,
> TSalm
>
>
> ------------------------------------------------------------------------------
> Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
> trial. Simplify your report design, integration and deployment - and focus on
> what you do best, core application coding. Discover what's new with
> Crystal Reports now.  http://p.sf.net/sfu/bobj-july
> _______________________________________________
> MinGW-users mailing list
> [hidden email]
>
> This list observes the Etiquette found at
> http://www.mingw.org/Mailing_Lists.
> We ask that you be polite and do the same.
>
> Most annoying abuses are:
> 1) Top posting
> 2) Thread hijacking
> 3) HTML/MIME encoded mail
> 4) Improper quoting
> 5) Improper trimming
> _______________________________________________
> You may change your MinGW Account Options or unsubscribe at:
> https://lists.sourceforge.net/lists/listinfo/mingw-users
>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete an element in a map while iterate on it

Andreas Rönnquist-3
In reply to this post by TSalm
On Wed, 09 Sep 2009 22:44:34 +0200
TSalm <[hidden email]> 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 --------------------*/
try somethinr like

for (map<string,string>::iterator it = myMap.begin(); it!=myMap.end();)
    {
       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 ) ;
       } else {
          ++it;
       }
    }
 
 (I have simply moved the ++it to the end of the loop, and made it called only when an object isn't erased. - erase itself updates the iterator, so no need to do it when erase is called.)


--
Andreas Rönnquist <[hidden email]>

------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete an element in a map while iterate on it

Tuomo Latto
In reply to this post by TSalm
TSalm wrote:
> 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 :-)

I think not but I could be wrong.

> Here is my question : is there a way to delete an element of a map while
> iterate on it ?

http://www.sgi.com/tech/stl/


--
Tuomo

... Open mouth. Insert Foot. Echo internationally.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete an element in a map while iterate on it

TSalm
In reply to this post by Linlin Yan
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<[hidden email]> 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 --------------------*/
>>
>>    




------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete an element in a map while iterate on it

Роман Донченко
TSalm <[hidden email]> писал(а) в своём письме Thu, 10  
Sep 2009 01:29:23 +0400:

>> 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++;
>>     }
>> }
> Thanks, this work perfectly !
> And thanks too for your advice about comp.lang.c++.

Also of note is that in C++9x you will likely be able to delete by saying  
"it = myMap.erase(it)", as the definition of erase is amended to make it  
return an iterator to the next element. Until then, do the above.

Roman.


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users
Reply | Threaded
Open this post in threaded view
|

Re: Delete an element in a map while iterate on it

TSalm
Роман Донченко a écrit :

> TSalm <[hidden email]> писал(а) в своём письме Thu, 10  
> Sep 2009 01:29:23 +0400:
>
>  
>>> 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++;
>>>     }
>>> }
>>>      
>> Thanks, this work perfectly !
>> And thanks too for your advice about comp.lang.c++.
>>    
>
> Also of note is that in C++9x you will likely be able to delete by saying  
> "it = myMap.erase(it)", as the definition of erase is amended to make it  
> return an iterator to the next element. Until then, do the above.
>
>  
Interesting. Is there a version of MingW featuring some C++0x
functionnalities ?


------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now.  http://p.sf.net/sfu/bobj-july
_______________________________________________
MinGW-users mailing list
[hidden email]

This list observes the Etiquette found at
http://www.mingw.org/Mailing_Lists.
We ask that you be polite and do the same.

Most annoying abuses are:
1) Top posting
2) Thread hijacking
3) HTML/MIME encoded mail
4) Improper quoting
5) Improper trimming
_______________________________________________
You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users