0

I have a map of maps and during the iteration i need to delete the iterator like i show in the follow code. What is the best way to do this?

 for (Map1_t::iterator itOutMap = map1.begin(); itOutMap != map1.end(); ++ itOutMap)
  {
    for (Map2_t::iterator itInMap = itOutMap->second.begin(); itInMap != itOutMap->second.end(); ++itInMap)
    {
      if (itInMap->first ==  USER_REQ_TYPE)
      {
        // need to delete the current itInMap
      }
    }
  }
0

1 Answer 1

1

You can use std::map::erase() http://en.cppreference.com/w/cpp/container/map/erase the example in that page does exactly what you are asking

for (Map1_t::iterator itOutMap = map1.begin(); itOutMap != map1.end(); ++ itOutMap)
  {
    for (Map2_t::iterator itInMap = itOutMap->second.begin(); itInMap != itOutMap->second.end(); )
    {
      if (itInMap->first ==  USER_REQ_TYPE)
      {
        itInMap=itOutMap->second.erase(itInMap);
      }
      else ++itInMap;
    }

  }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.