You can simply modify the element directly through the iterator (which points directly to the corresponding element):
foreach(iter, eMap) { if (compareByNameAge(name, age, iter->first)) iter->second.salary = 1000; }
for more complex modifications, you can take the value from the link:
EmployeeDetail& det = iter->second; det.salary = 1000;
In C ++, you usually cannot change the collection during iteration, but that means you cannot delete / add items. Changing existing elements is generally excellent in C ++ 11. What you cannot change is the key in map and any part of the element in set , but it is const in C ++ 11 anyway, so you cannot change. In C ++ 03, you need to remember that you do not need to change the key part of an element in set .
source share