I don't understand why I get segmentation fault on this code inside the "if" conditional, and when is out out it, it works perfectly..
The map cidades starts empty.
Here is the code:
int i,cont = 0; // counters
string line;
map<string,int> cidades;
... open a txt file here ...
while(getline(element, line, ',')){
if(i == 1) {
std::map<std::string, int>::iterator it = cidades.find(line);
// get the error when I try to print here
// without the line bellow, there is no error
//it looks like the if is always true
cout << "ele: " << it->first << " key: " << it->second;
if(it == cidades.end()){ // insert map
cidades.insert(pair<string,int>(line,cont));
c1 = cont;
}else{ // get the key
c1 = cidades.at(line);
}
}
cont++; i++;
}
But when I put the same code out of the conditionals above.. it works
string a = "teste"; // taken from the txt and added into the map
std::map<std::string, int>::iterator it = cidades.find(a);
cout << "ele: " << it->first << " chave: " << it->second;
it prints teste and the key... but when I try to print inside the if I got segmentation fault(core dumped).
Aucun commentaire:
Enregistrer un commentaire