1
// Erase the missing items
vector<AlignedFDRData>::size_type StandardNum = FDRFreq.at(0).fData.size();
vector<AlignedFDRData>::iterator iter = FDRFreq.begin(); 
while (iter != FDRFreq.end()){
    if( iter->fData.size() < StandardNum){
        FDRFreq.erase(iter);
    }
    else{
        ++iter;
    }
}

This part is used to erase the FDRFreq vector item, in which the data length is smaller than the standard number, but the debug assertion failed: vector iterators incompatible. I am a green hand in C++ STL, thanks for your kindly help.

Jiahui Guo
  • 135
  • 2
  • 10
  • You invalidate your iterator if you `erase`, think about it. you should store the iterator that is returned from the call to erase like Mahmoud has answered – EdChum Apr 22 '12 at 20:32

2 Answers2

8

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));
pmr
  • 58,701
  • 10
  • 113
  • 156
7

Your code needs to become

while (iter != FDRFreq.end()){
    if( iter->fData.size() < StandardNum){
        iter = FDRFreq.erase(iter);
    }
    else{
        ++iter;
    }
}

"vector iterators incompatible" means that the iterator you're using has been invalidated - that is to say, there is no guarantee that the elements it points to still exist at that memory location. An erase of a vector element invalidates the iterators following that location. .erase returns a new, valid iterator you can use instead.

If you're new to STL, I highly recommend you read Scott Myer's Effective STL (and Effective C++, while you're at it)

Mahmoud Al-Qudsi
  • 28,357
  • 12
  • 85
  • 125
  • 1
    This is not the idiomatic way for a reason. It will trigger as much reallocation and copies as elements are going to be removed. Don't use it. – pmr Apr 22 '12 at 20:35
  • Yes, but that is the correct answer to the question the OP asked. I added the recommendation to read Effective STL which provides the correct method. – Mahmoud Al-Qudsi Apr 22 '12 at 20:36
  • 1
    Point taken. Still, I don't think a reference to a single book is helpful here. OP is not going to find the information he immediately needs and his code will continue to be wrong (Yes, I consider horrible performance to be wrong code). – pmr Apr 22 '12 at 20:41
  • Thanks for all of you. I will read the book recommended here^^. – Jiahui Guo Apr 22 '12 at 20:52
  • It's not really a book, it's 25 "correct ways" of doing things. Very short, you don't have to read in order, and very to the point. – Mahmoud Al-Qudsi Apr 22 '12 at 20:53