Iterator invalidation in boost::unordered_map

前端 未结 1 850
温柔的废话
温柔的废话 2021-01-13 17:54

I am using boost::unordered_map as follows

typedef boost::shared_ptr PtrWriter;
typedef std::list PtrLis         


        
相关标签:
1条回答
  • 2021-01-13 18:33

    The erase() operation will invalidate the iterator. However, it also returns a valid iterator to the next element. So you can use something like the following:

    for(auto it = instrMap.begin(); it != instrMap.end();)
    {
         auto key = it->first();
         auto list& = it->second();    
         //Make some change to an element in list 
    
          if(list.empty())
          {
                it = instMap.erase(it); 
          }
          else {
                ++it;
          }
    }
    
    0 讨论(0)
提交回复
热议问题