We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Because you didn't erase the iterator while you loop through the whole entries. You shouldn't has this issue. I just tried, it works just fine.
int main() {
map m;
int n, y;
cin >> n;
while(n--){
int t;
cin >> t; string x; cin >> x;
map::iterator it = (m.find(x));
if(t == 1){
cin >> y;
if(it != m.end())
it->second += y;
else m.insert(make_pair(x, y));
} else if(t == 2){
m.erase(it); //IF x is used instead of it, no problem. it causes seg. fault in most of test cases. }
}else{
if(it != m.end()){
cout << it->second << endl;
} else{
cout << 0 << endl;
}
}
}
return 0;
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Maps-STL
You are viewing a single comment's thread. Return to all comments →
Because you didn't erase the iterator while you loop through the whole entries. You shouldn't has this issue. I just tried, it works just fine.
int main() { map m; int n, y; cin >> n; while(n--){ int t; cin >> t; string x; cin >> x; map::iterator it = (m.find(x)); if(t == 1){ cin >> y; if(it != m.end()) it->second += y; else m.insert(make_pair(x, y)); } else if(t == 2){ m.erase(it); //IF x is used instead of it, no problem. it causes seg. fault in most of test cases. } }else{ if(it != m.end()){ cout << it->second << endl; } else{ cout << 0 << endl; } } } return 0; }