迭代并从std :: map中删除一些元素时出现分段错误?

时间:2016-07-25 16:02:30

标签: c++ c++11 stdmap

我正在使用以下几个std::map类型的数据容器:

    std::map<int, std::vector< cv::Point > > mapGoodContours;
    std::map<int, EllipseProperties> ellipsePropertiesMap;
    std::map<int, float> m_markerRadiusMap;
    std::map<int,cv::Point2f> markers;//This is part of another class

我遍历这些容器并在这些元素满足某些条件后删除一些元素,如下面的代码所示。

    auto radiusMapCounter = m_markerRadiusMap.begin();
    auto markerCounter = frames.markers.begin();
    auto goodContoursCounter = mapGoodContours.begin();

    if(m_markerRadiusMap.size()==ellipsePropertiesMap.size() && frames.markers.size()==ellipsePropertiesMap.size()
            && mapGoodContours.size()==ellipsePropertiesMap.size())
    {
        for(auto ellipsePropertyCounter = ellipsePropertiesMap.begin(); ellipsePropertyCounter != ellipsePropertiesMap.end(); ellipsePropertyCounter++)
        {

            float upperLimit = (float)m_upperFactor*(float)ellipsePropertyCounter->second.radius;
            float lowerLimit = (float)m_lowerFactor*(float)ellipsePropertyCounter->second.radius;
            if(ellipsePropertyCounter->second.minDistanceFromOtherEllipse>upperLimit
                        || ellipsePropertyCounter->second.minDistanceFromOtherEllipse<lowerLimit)
            {
                 ellipsePropertiesMap.erase(ellipsePropertyCounter);
                 m_markerRadiusMap.erase(radiusMapCounter);
                 frames.markers.erase(markerCounter);
                 mapGoodContours.erase(goodContoursCounter);
            }
            else
            {
                 smallContours.push_back(goodContoursCounter->second);
            }

            radiusMapCounter++;
            markerCounter++;
            goodContoursCounter++;
        }
    }

我很困惑地发现,有时我会出现图像中显示的分段错误。 enter image description here该错误专门指向代码为radiusMapCounter++;

的行

我做错了什么?

2 个答案:

答案 0 :(得分:3)

擦除从容器指向的元素后,无法增加迭代器。创建迭代器的副本,递增它,通过副本删除元素。

如果您使用的是C ++ 11或更高版本,std::map::erase(...)将返回最后一个删除元素后面的迭代器,因此您可以使用它而不是递增无效元素。在这种情况下,无需创建在erase中使用的迭代器副本。

for(auto ellipsePropertyCounter = ellipsePropertiesMap.begin();
    ellipsePropertyCounter != ellipsePropertiesMap.end();
    /* do not increment iterator here */)
{

    float upperLimit = (float)m_upperFactor*(float)ellipsePropertyCounter->second.radius;
    float lowerLimit = (float)m_lowerFactor*(float)ellipsePropertyCounter->second.radius;
    if(ellipsePropertyCounter->second.minDistanceFromOtherEllipse>upperLimit
                || ellipsePropertyCounter->second.minDistanceFromOtherEllipse<lowerLimit)
    {
         ellipsePropertyCounter = ellipsePropertiesMap.erase(ellipsePropertyCounter);
         radiusMapCounter = m_markerRadiusMap.erase(radiusMapCounter);
         markerCounter = frames.markers.erase(markerCounter);
         goodContoursCounter = mapGoodContours.erase(goodContoursCounter);
    }
    else
    {
         smallContours.push_back(goodContoursCounter->second);

         radiusMapCounter++;
         markerCounter++;
         goodContoursCounter++;
         ellipsePropertyCounter++; // increment loop iterator only in 'else' case
    }
}

答案 1 :(得分:0)

这是一个C ++ 17解决方案。

第一个indexer,它是C ++ 14,允许您在使用时无需特殊用途帮助程序内联创建和解压缩索引包:

template<class=void,std::size_t...Is>
void indexer( std::index_sequence<Is>... ) {
  return [](auto&& f) {
    using discard=int[];
    (void)discard{0,((
      f( std::integral_constant<std::size_t, Is>{} )
    ),void(),0)...};
  };
}
template<std::size_t N>
void indexer() {
  return indexer( std::make_index_sequence<N>{} );
}

现在,erase_if,SFINAE应限制为仅适用于关联(也可能是其他基于节点的)容器:

template<class Test, class...Maps>
bool erase_if( Test&& test, Maps&&...maps) {
  using std::begin; using std::end;
  std::tuple< decltype(begin(maps))... > its;
  std::tuple< decltype(begin(maps))... > ends;
  auto m_tup = std::tie(maps...);
  auto index = indexer<sizeof...(maps)>;
  index(
    [&](auto i){
      std::get<i>(its) = begin( std::get<i>(m_tup) );
      std::get<i>(ends) = end( std::get<i>(m_tup) );
    }
  );
  while (its != ends) {
    auto deref_then_test = [&test](auto...its) {
      return test( (*its)... );
    };
    if (std::apply(deref_then_test,its)) {
      index(
        [&](auto i){
          std::get<i>(its) = std::get<i>(m_tup).erase(std::get<i>(its));
        }
      );
    } else {
      index(
        [&](auto i){++std::get<i>(its);}
      );
    }
  }
}

这使您可以根据对其中一个容器的测试从多个容器中删除。

未经测试的代码,可能是错误的。