Today, in the process of writing a program, the following accident occurred in this situation, I did not find a reason for the moment, very depressed.
#include <iostream> using namespace std;
#include <map>
#include <string>
int main ()
{
Map<int,string> MS;
Ms.insert (Make_pair (1, "Huzhongsan"));
int i=2;
while (i--)
{
Map<int,string>::iterator it = ms.find (i)//here, when I value is 0 o'clock, not found, will return Ms.end ()
Ms.erase (IT), if it is ms.end (), there will be accidents, here will be blocked, blocking the dead, the program will stop here.
}
return 0;
}
How to avoid the above situation, this is good to do, add a judgment on the good, attention, this situation once appeared, if you write is a very large program, there may not be a good one to find out the bug,
And a lot of us (like my rookie), now the internal implementation of the STL map is not very clear, encountered this error, it is difficult to explain, only by guessing, and then programming verification, some
Time really is no reason to speak, hehe
Here's a way to avoid this happening
#include <iostream>
using namespace Std;
#include <map>
#include <string>
int main ()
{
Map<int,string> MS;
Ms.insert (Make_pair (1, "Huzhongsan"));
int i=2;
while (i--)
{
Map<int,string>::iterator it = ms.find (i);
if (It!=ms.end ())//Just so, add a judgment on the OK ...
Ms.erase (IT);
}
return 0;
}