This is because the
map::operator [][
^] is inserting a new element when the key does not exist so far and sets the value to zero. As a result the follwing call to
map::insert()[
^] will always fail.
If you want to get a value without automatic insert, use
map::at()
. But note that this will throw en exception if the key does not exist.
Getting the settings you want is much simpler:
while (x > 0)
{
int temp = x % 10;
container[temp]++;
x = x / 10;
}
If the key does not exist, it is created with value zero and then incremented. If it exists already, it is just incremented.