[英]Segmentation fault while iterating through and erasing some elements from std::map?
我正在使用以下幾個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++;
}
}
我很困惑,發現有時我會遇到如圖所示的分割錯誤。 故障具體指向代碼為
radiusMapCounter++;
我究竟做錯了什么?
從容器中刪除它指向的元素后,您將無法遞增迭代器。 創建迭代器的副本,對其進行遞增,然后通過副本刪除元素。
如果您使用的是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
}
}
這是C ++ 17解決方案。
First 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>{} );
}
現在,應該在SFINAE erase_if
其設為僅限於關聯(可能還有其他基於節點的)容器的erase_if
:
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);}
);
}
}
}
這樣一來,您就可以根據多個容器之一對它們進行測試,從而從多個容器中擦除它們。
代碼未經測試,可能是錯字。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.