C++ – Why did I get a Segmentation Fault with a map insert


I want to insert a pair< string, vector<float> > into a map, first it works, but after several loops, it cannot insert any more and throw me a segmentation fault. Can anybody give a possible reason?

Btw: I first read a file and generate the map (about 200,000 elements) and I read another file and update the old map. the error occurs while the updating step.

Can anybody help me with the info I gave above? Thanks a lot

The code is pretty long…..I just erase the previous key and then insert a new one, it seems not complicated…..but drives me crazy….could you guess what happened here?

Thanks A lot for all your answers! And I found it is really a good place for solving problems. Thanks again, I'll try to simplify my codes and add it here today or tomorrow.

Update: I used the code from MSN and it works, thanks a lot that you solved my problem without seeing my code……also many thanks to other kind-hearted people here! However, i can only choose one as the answer.

Best Solution

Are you inserting using the iterator you called erase() on? Or using that iterator in any way? After erase(p) is called, p is invalidated.