Qt 6.1 introduced the method removeIf(Predicate Pred)
to a number of its collection classes: QByteArray
, QHash
, QList
, QMap
, QMultiHash
, QMultiMap
, QString
and QVarLengthArray
.
But how do I write a predicate?
Let's take a QHash
example:
struct MishMash {
int i;
double d;
QString str;
enum Status { Inactive=0, Starting, Going, Stopping };
Status status;
};
QHash<QString, MishMash> myHash;
// ... fill myHash with key-value pairs
// Now remove elements where myHash[key].status == MishMash::Status::Inactive;
myHash.removeIf(???);
CodePudding user response:
From the documentation...
The function supports predicates which take either an argument of type QHash<Key, T>::iterator, or an argument of type std::pair<const Key &, T &>.
That being the case, you should be able to use a lambda
something along the lines of (untested)...
myHash.removeIf(
[](QHash<QString, MishMash>::iterator i)
{
return i.value().status == MishMash::Status::Inactive;
}
);