I am experienced in C/C++ but pretty much a newbie in C#. My question is pretty simple. Say we have a hash table with integer keys and values and we want to increment all the values in the hash table by 1. We prefer to accomplish this with O(1) extra memory.
Below is one solution, which, to my opinion, is somehow ugly. Is there other way to make it looks more decent?
Dictionary<int, int> dict = new Dictionary<int, int>();
for (int i = 0; i < dict.Count; ++i)
{
dict[dict.Keys.ElementAt(i)]++;
}
PS: I heard that foreach
is read-only in C#. But, is there any way like for(auto it& : dict) it.second++
in C++ that I can use to still accomplish this task in C#?