I currently have lots of code which looks like this:
std::unordered_map<int,int> my_dict;
.
.
.
// If the key does exist in the dictionary
if(my_dict.count(key) == 1){
my_dict[key] = value;
}
// If its a new key
else{
my_dict.insert(std::make_pair(key,value));
}
Is there any way I can speed this up by just overwriting the value every time?
You just do (for map
and unordered_map
)
mydict[key]=value;