Is it correctly possible to walk sequently through QMap with help of iterators, and doing such actions: removing some items and adding new ones?
For example:
for( QMap<key_t,val_t>::iterator it = map.begin();
it != map.end();
++it )
{
if( it->value == something )
{
map.erase(it);
map.insert(it->key+10,it->value);
}
}
It seems, that nothing will be done wrong, I’m asking to be sure. (I have no enough time to check it).
UPD Will solve with QMap::unite():
for( QMap<key_t,val_t>::iterator it = map.begin();
it != map.end();
++it )
{
if( it->value == something )
{
tmp_map.insert(it->key+10,it->value);
map.erase(it);
}
}
map.unite(tmp_map);
Thanks for answers!
Think about it a little while… You are iterating over a collection, removing an item in the middle and adding another item somewhere else. Will the iterators still be correct? Will the “next” iterator really be the next item?
In general it is not a good idea to change a collection you are iterating over. If you need to then use a temporary collection and copy selected items over to that, and clear the real collection and move the items from the temporary collection over to the real one.
In your case though, why not useQMap::findto search forsomething, and if found erase it and add the new item, and do it in a loop untilsomethingis not found anymore?