I’m trying to increase my usage of STL algorithms to avoid reinventing wheels. I use JUCE’s OwnedArray class in certain places, but I’ve found that the .end() and .begin() methods in Array and OwnedArray make using STL algorithms quite seamless.
Now I’ve run into a problem. I have an OwnedArray where I want to remove elements that match a certain criteria. This is a perfect fit for std::remove_if, which lets you use a lambda predicate, very convenient. After remove_if (which just moves elements which match the predicate to the end and returns an iterator to the point where the matched elements begin) the normal STL thing would be to call erase() on the container. I have replaced this with a call to ownedArray.removeLast (ownedArray.end() - firstMatchedElementIterator).
This appears to work fine, but later when I call a virtual method on one of the remaining elements (it’s a polymorphous list of objects) I get a bad access error. Is the STL algorithm moving pointer elements somehow causing the program to think the object is the wrong type? Or is there something wrong with the pointer arithmetic I’m doing on the iterators?