CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    Jun 2009
    Posts
    118

    Question how to erase entries in a map on-the-fly ?

    Hi All,

    I think that it is not legitimate to erase entries in a map in the following way:

    Code:
        MyMap temp;
    
        //fill in temp here
    
        MyMap::iterator it; //MyMap is a typedef of STL map
    
        for(it = temp.begin(); it != temp.end(); it++)
        {
            if( condition )
            {
                 temp.erase(it->first); //erase by key
            }
        }
    The purpose is to transform the above code into one so that entries meeting a certain condition can be erased from map on one pass.

    Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how to erase entries in a map on-the-fly ?

    Quote Originally Posted by aryan1
    The purpose is to transform the above code into one so that entries meeting a certain condition can be erased from map on one pass.
    I shall paraphrase the example that I often refer to, from Josuttis' The C++ Standard Library:
    Code:
    for (it = temp.begin(); it != temp.end();)
    {
        if (condition)
        {
             temp.erase(it++);
        }
        else
        {
            ++it;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to erase entries in a map on-the-fly ?

    Quote Originally Posted by aryan1 View Post
    Hi All,

    I think that it is not legitimate to erase entries in a map in the following way:

    Code:
        MyMap temp;
    
        //fill in temp here
    
        MyMap::iterator it; //MyMap is a typedef of STL map
    
        for(it = temp.begin(); it != temp.end(); it++)
        {
            if( condition )
            {
                 temp.erase(it->first); //erase by key
            }
        }
    The purpose is to transform the above code into one so that entries meeting a certain condition can be erased from map on one pass.

    Thanks.
    Code:
    #include <algorithm>
    bool SomeCondition(const MyMap::value_type& v)
    {
      //...return true if v is to be erased, false otherwise
    }
    
    temp.erase(std::remove_if(temp.begin(), temp.end(), SomeCondition),
                       temp.end());
    Regards,

    Paul McKenzie

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: how to erase entries in a map on-the-fly ?

    Paul McKenzie : it is a std::map, so std::remove_if is not appropriate.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to erase entries in a map on-the-fly ?

    Quote Originally Posted by laserlight View Post
    Paul McKenzie : it is a std::map, so std::remove_if is not appropriate.
    Yes, I was going to say the same thing. 95% sure it compiles, and 95% sure it messes up your map.

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: how to erase entries in a map on-the-fly ?

    Some people will show you this form:

    Code:
    for (it = temp.begin(); it != temp.end();)
    {
        if (condition)
        {
             it = temp.erase(it);
        }
        else
        {
            ++it;
        }
    }
    While this is more straight forward and easier to understand, it does not conform to the standard. erase does not actually return an iterator.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: how to erase entries in a map on-the-fly ?

    Quote Originally Posted by monarch_dodra View Post
    Yes, I was going to say the same thing. 95% sure it compiles, and 95% sure it messes up your map.
    Yes, it won't work. Only forward iterators should be used, so associative containers seem to be off-limits.

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: how to erase entries in a map on-the-fly ?

    To quote "Effective STL"

    Use this for std::vector, std::string & std:eque

    Code:
    for (it = temp.begin(); it != temp.end();)
    {
        if (condition)
        {
             it = temp.erase(it);
        }
        else
        {
            ++it;
        }
    }
    For others use

    Code:
    for (it = temp.begin(); it != temp.end();)
    {
        if (condition)
        {
             temp.erase(it++);
        }
        else
        {
            ++it;
        }
    }
    For std::list use either.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured