C++ STL vector iterators incompatible

后端 未结 2 1224
予麋鹿
予麋鹿 2020-12-21 22:41
// Erase the missing items
vector::size_type StandardNum = FDRFreq.at(0).fData.size();
vector::iterator iter = FDRFreq.be         


        
2条回答
  •  礼貌的吻别
    2020-12-21 22:59

    Your problem is iterator invalidation after the call to std::erase. The warning is triggered by an iterator debugging extensions in your standard library implementation. erase returns an iterator to the new valid location after the erase element and you continue iterating from there. However, this is still very inefficient.

    Use the Erase-Remove Idiom to remove data with a predicate from a vector.

    FDRFreq.erase(std::remove_if(
                    begin(FDRFreq), end(FDRFreq), 
                    [&StandardNum](const AlignedFDRData& x) { 
                      return fData.size() > StandardNum; }),
                  end(FDRFreq));
    

提交回复
热议问题