std::map iterator and clearing

From: Kelly Mandrake (atapi103_at_gmail.com)
Date: 02/28/05


Date: 27 Feb 2005 19:54:37 -0800

I have been learning about STL's map class. I see that I can clear a
hashmap by useing the method clear, however I decided to try to clear
the hash with a for loop. Since erase can take an iterator as a
position I thought I could do something like this:

hashTable::iterator i;

for (i = phoneBook.begin(); i != phoneBook.end(); i++)
{
cout << i->first << endl;
phoneBook.erase(i);
}

I get an access violation, I think the reason is that the condition is
never false , and thus the loop continues to now access memory beyond
phoneBook. But I cannot see why. If i comment out the erase line I am
ok, but what I am trying to do is to clear the phoneBook. Without
useing clear method.

Could somebody give me a hint, is it true the condition is never false?
 What could possibly be causeing that.