I\'m taking a self-study course for C++, learning how the Standard Library works, and I want to understand how this code that uses for_each
works, particularly
You can't simply change the key of an element in a key-value store kind of a data structure. (In case of a set, it's only key, no value.)
If you would, the element's position in the underlying container might need to be adjusted, based on the recomputed hash value in case of an unordered hash table, or a different sorting position in a binary search tree / ordered list.
Therefore the API of the std::set
requires you to erase and reinsert the item in this case.
Which the answers to existing questions in this direction already state: