No no. A loop-based range is used to access each element of the container once.
Each time an element is removed from the container, iterators in or after the element to be erased are no longer valid (and range implementation is given β this is a problem).
You should use a regular loop (or while ) while if you need to change the container as you go.
If you want to remove elements for which the predicate returns true , a good way:
m_Connections.erase( std::remove_if(m_Connections.begin(), m_Connections.end(), [](Type elem) { return predicate(elem); }), m_Connections.end());
std :: remove_if does not mix iteration logic with predicate.
source share