I had a problem calling the following code:
#include<deque> using namespace std; deque<int> deq = {0,1,2,3,4,5,6,7,8}; for(auto it = deq.begin(); it != deq.end(); it++){ if(*it%2 == 0) deq.erase(it); }
resulting in a segmentation error. Having studied the problem, I found that the problem is that the STL controls iterators for deques: if the element to be erased is closer to the end of the deque, the iterator used to indicate the element to be erased will now point to the NEXT element, but not the previous element like vector::iterator . I understand that changing the loop condition from it != deq.end() to it < deq.end() might solve the problem, but I just wonder if there is a way to go through and remove a specific element in deque in "standard form" so that the code can also be compatible with other types of containers.
source share