gpt4 book ai didi

qt - 如何从插入到QMap中的字符串中找到对应的键值?

转载 作者:行者123 更新时间:2023-12-02 17:40:25 24 4
gpt4 key购买 nike

我有以下数据结构。

QMap<int,QString> map;

map.insert(0x01,"HELLO");
map.insert(0x02,"FELLOW");
map.insert(0x83,"NESTLE");
map.insert(0x044,"KITKAT");

QString str="NESTLE";

我的要求是在传递字符串 - NESTLE 时检索值 -0x83。基本上,基于 str 值,我想获得相应的键值,即本例中的 0x83。我该怎么做?

最佳答案

我会这样做:

[..]
QString str = "NESTLE";
int key = getKey(map, str); // should return 0x83 in your example.
[..]

int getKey(const QMap &map, const QString &value)
{
foreach (QString v, map) {
if (v == value)
return map.key(value);
}
return -1;
}

更新:

或者可以通过以下方式简单地找到 key :

int key = map.key("NESTLE");

关于qt - 如何从插入到QMap中的字符串中找到对应的键值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21606159/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com