gpt4 book ai didi

c++ - map 数组帮助c++/qt

转载 作者:行者123 更新时间:2023-11-28 01:09:25 24 4
gpt4 key购买 nike

我不知道我这样做是否正确,C++ 不是我的母语。

QMap < QString, QString > *mapArray;
QMap < QString, QString > map[10];
mapArray = map;

mapArray[0].insert("key1", "value1");
mapArray[0].insert("key1-1", "value1-1");
mapArray[1].insert("key2", "value2");
mapArray[2].insert("key3", "value3");
mapArray[2].insert("key3-1", "value3-1"); ...

现在我想遍历整个 mapArray。执行此操作的最佳方法是什么?

最佳答案

您不需要指针。去掉它,只用 map 代替它:

QMap < QString, QString > map[10];

map[0].insert("key1", "value1");
map[0].insert("key1-1", "value1-1");
map[1].insert("key2", "value2");
map[2].insert("key3", "value3");
map[2].insert("key3-1", "value3-1"); ...

要遍历它们,您需要遍历数组,然后遍历映射。

for (int i = 0; i < 10; ++i)
{
for (QMap<QString, QString>::iterator it = map[i].begin(); it != map[i].end(); ++it)
{
const QString& key = it->first;
QString& value = it->second;
...
}
}

如您所见,QMap 迭代器是键和值的QPair。您可以像我上面那样使用 firstseconds 分别检索它们。

关于c++ - map 数组帮助c++/qt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4199912/

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