Remove Elements from an Unordered Map Fulfilling a Predicate

The answer is no (you can’t use remove_if on associative containers). You need to do a simple loop; the erase(iterator) member now returns the next valid iterator – so your loop becomes:

for(auto it = begin(m_map); it != end(m_map);)
{
  if (it->second == 0)
  {
    it = m_map.erase(it); // previously this was something like m_map.erase(it++);
  }
  else
    ++it;
}

Leave a Comment

tech