mercredi 20 avril 2016

C - Why is program crashing with if statement inside map iterator?

I am new to C++ and I am trying to iterate through the map while passing an if statement through. However the program crashes.

Please help me fix the program.

#include <bits/stdc++.h>
#include <iostream>
#include <set>
#include <string>
#include <iterator>

using namespace std;

int main()
{
    std::map<int,int> h;
    std::map<int,int>::iterator it;

    h[1] = 2;
    h[4] = 5;
    for(it = h.begin(); it !=h.end(); it++){
        if (it->second > 4){
            h.erase(it->first);
        }
}

Aucun commentaire:

Enregistrer un commentaire