How to update the value of a key in std::map
after using the find
method?
I have a map and iterator declaration like this:
If you already know the key, you can directly update the value at that key using m[key] = new_value
Here is a sample code that might help:
map m;
for(int i=0; i<5; i++)
m[i] = i;
for(auto it=m.begin(); it!=m.end(); it++)
cout<second<<" ";
//Output: 0 1 2 3 4
m[4] = 7; //updating value at key 4 here
cout<<"\n"; //Change line
for(auto it=m.begin(); it!=m.end(); it++)
cout<second<<" ";
// Output: 0 1 2 3 7