Qt 6.1 introduced the method removeIf(Predicate Pred)的集合类有很多:QByteArray、QHash、QList、QMap、QMultiHash、QMultiMap、QString和QVarLengthArray。
但是我怎么写谓词呢?
让我们以一个QHash为例:
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(???);发布于 2022-05-24 11:54:55
从文件中..。
函数支持谓词,这些谓词要么采用QHash::iterator类型的参数,要么采用类型std::pair的参数。
在这种情况下,您应该能够按照(未经测试)的方式使用lambda .
myHash.removeIf(
[](QHash<QString, MishMash>::iterator i)
{
return i.value().status == MishMash::Status::Inactive;
}
);https://stackoverflow.com/questions/72362234
复制相似问题