I use QHash as a container and I have a task to remove all items that satisfy the predicate. At first I thought to use the Erase-remove idiom it turned out that QHash has no option to delete the range, but only a function to delete a single element through the iterator.
std::unordered_map (which is conceptually close to the Qt's QHash) has the function of removing the range.
This implies a question: why QHash does not have a similar function and how to the best way remove items from the QHash satisfying the predicate?
On the basis of the comments, it turns out that the erase-remove idiom does not apply to QHash container.
Therefore, given the description of the QHash::erase, in particular that it does not violate the order of items in the hash
Unlike remove() and take(), this function never causes QHash to rehash its internal data structure. This means that it can safely be called while iterating, and won't affect the order of items in the hash.
we have the following code to remove the elements that satisfy the predicate:
for( auto it = hash.begin(); it != hash.end(); )
{
if( pred(*it) )
{
it = hash.erase(it);
} else {
++it;
}
}