Fixes undefined behavior from erasing an end iterator

Fixes #810.