This is a very common issue.
Because after you erase an element the position j pointed will skip one element due to the j++ on the for loop.
the easiest solution to solve the problem based on your code is to add j-- after generation.erase(iter):
generation.erase(iter);
j--;