|
From: Linlin Y. <yan...@gm...> - 2009-09-09 21:15:41
|
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 --------------------*/
>
> 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
> Min...@li...
>
> 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
>
|