Calling erase while iterating over the multiset is a bit tricky. I recommend you find a way so that you don't have to call erase, or a totally different way where you don't have to use multiset (hint: you can do what you're trying to do very easily using std::map).