如何使用qhash :: remove(谓词pred)

发布于 2025-01-31 17:26:11 字数 797 浏览 4 评论 0原文

qt 6.1引入了方法emove> emove> emovef(predicate pred) 到其许多集合类:qbyTearrayqhashqlistqmapqmultihhash < /code>,qmultimapqStringqvarlengthraray

但是如何写谓词?

让我们以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(???);

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(???);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伤痕我心 2025-02-07 17:26:11

从文档...

该函数支持谓词,该谓词采用类型的参数
qhash&lt; key,t&gt; :: iterator,或类型std :: pair&lt&const key&amp; ,,
t&amp;&gt;。

在这种情况下,您应该能够使用lambda按照(未经测试)...

myHash.removeIf(
    [](QHash<QString, MishMash>::iterator i)
    {
        return i.value().status == MishMash::Status::Inactive;
    }
);

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;
    }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文