如何在其他QMap中迭代QMap
How do iterate QMap in other QMap
我在寻找如何在另一个 QMap
中迭代一个 QMap
,例如:
QMap<int, QMap<int, QString>> map;
以前我使用简单的 C++ std::map
和以下代码并且有效:
for(auto it = this->liste.begin(); it != this->liste.end(); it++) {
for(auto itr = it->second.begin(); itr != it->second.end(); itr++) {
//It works !!!!!
//qDebug() << "First : " << itr->first;
//qDebug() << "Second : " << itr->second;
//d.setPath(itr->second);
//qDebug() << "Path :" << itr->second << " Prefix :" << this->prefix << " Nb :" << itr->first;
process(d.absolutePath(), this->prefix, itr->first);
this->liste.clear();
}
}
我的问题是如何使用 QMap
而不是 std::map
以便在带有 foreach
循环的 QTreeView
中使用 QMap
(特别是 QList
的物品)。
liste.first
和 liste.second
的替代品是什么?
编辑:
std::map<int, std::map<int, QString>> liste;
谢谢!
您可以使用 QMap::keys()
with QMap::value()
or QMap::operator[]
to iterate over the list to keys and then use keys to get the values. Another solution could be just to get an std::map
from QMap::toStdMap()
并使用 range-for
循环对其进行迭代。
您可能还想查看 QMap::uniqueKeys()
,具体取决于您的用例。
更新:
如 by cuda12, you can also make use of QMapIterator
or QMutableMapIterator
中所述,您需要使用 Java 风格的迭代器,但它的效率低于 STL 风格的迭代器。而且,它不能满足您使用 foreach
或 range-for
.
的要求
引用其 documentation:
The Java-style iterators are more high-level and easier to use than the STL-style iterators; on the other hand, they are slightly less efficient.
这是一个工作示例:
#include <QDebug>
#include <QMap>
#include <QMapIterator>
#include <QString>
int main()
{
using InnerMap = QMap<int, QString>;
using OuterMap = QMap<int, InnerMap>;
const OuterMap outerMap
{
{ 1, {{ 11, "a" }, { 12, "aa" }} },
{ 2, {{ 22, "b" }, { 21, "bb" }} },
{ 3, {{ 22, "c" }} }
};
qDebug() << "--- foreach (QMap) ---";
foreach ( const auto& outerKey, outerMap.keys() )
{
qDebug() << outerKey;
const auto& innerMap = outerMap[ outerKey ];
foreach ( const auto& innerKey, innerMap.keys() )
{
const auto& innerValue = innerMap[ innerKey ];
qDebug() << "\t{" << innerKey << "," << innerValue << "}";
}
}
qDebug() << "\n--- range-for (QMap -> std::map) ---";
const auto& m1 = outerMap.toStdMap();
for ( const auto& p1 : m1 )
{
qDebug() << p1.first;
const auto& m2 = p1.second.toStdMap();
for ( const auto& p2 : m2 )
{
qDebug() << "\t{" << p2.first << "," << p2.second << "}";
}
}
qDebug() << "\n--- while (QMapIterator) ---";
QMapIterator<int, InnerMap> outerIt{ outerMap };
while ( outerIt.hasNext() )
{
outerIt.next();
qDebug() << outerIt.key();
QMapIterator<int, QString> innerIt{ outerIt.value() };
while ( innerIt.hasNext() )
{
innerIt.next();
qDebug() << "\t{" << innerIt.key() << "," << innerIt.value() << "}";
}
}
return 0;
}
输出:
--- foreach (QMap) ---
1
{ 11 , "a" }
{ 12 , "aa" }
2
{ 21 , "bb" }
{ 22 , "b" }
3
{ 22 , "c" }
--- range-for (QMap -> std::map) ---
1
{ 11 , "a" }
{ 12 , "aa" }
2
{ 21 , "bb" }
{ 22 , "b" }
3
{ 22 , "c" }
--- while (QMapIterator) ---
1
{ 11 , "a" }
{ 12 , "aa" }
2
{ 21 , "bb" }
{ 22 , "b" }
3
{ 22 , "c" }
我会使用 QMapIterator Class。请参阅文档示例:
QMap<int, QWidget*> map;
// ...
QMapIterator<int, QWidget*> i(map);
while (i.hasNext()) {
i.next();
qDebug() << i.key() << ": " << i.value();
}
我在寻找如何在另一个 QMap
中迭代一个 QMap
,例如:
QMap<int, QMap<int, QString>> map;
以前我使用简单的 C++ std::map
和以下代码并且有效:
for(auto it = this->liste.begin(); it != this->liste.end(); it++) {
for(auto itr = it->second.begin(); itr != it->second.end(); itr++) {
//It works !!!!!
//qDebug() << "First : " << itr->first;
//qDebug() << "Second : " << itr->second;
//d.setPath(itr->second);
//qDebug() << "Path :" << itr->second << " Prefix :" << this->prefix << " Nb :" << itr->first;
process(d.absolutePath(), this->prefix, itr->first);
this->liste.clear();
}
}
我的问题是如何使用 QMap
而不是 std::map
以便在带有 foreach
循环的 QTreeView
中使用 QMap
(特别是 QList
的物品)。
liste.first
和 liste.second
的替代品是什么?
编辑:
std::map<int, std::map<int, QString>> liste;
谢谢!
您可以使用 QMap::keys()
with QMap::value()
or QMap::operator[]
to iterate over the list to keys and then use keys to get the values. Another solution could be just to get an std::map
from QMap::toStdMap()
并使用 range-for
循环对其进行迭代。
您可能还想查看 QMap::uniqueKeys()
,具体取决于您的用例。
更新:
如 QMapIterator
or QMutableMapIterator
中所述,您需要使用 Java 风格的迭代器,但它的效率低于 STL 风格的迭代器。而且,它不能满足您使用 foreach
或 range-for
.
引用其 documentation:
The Java-style iterators are more high-level and easier to use than the STL-style iterators; on the other hand, they are slightly less efficient.
这是一个工作示例:
#include <QDebug>
#include <QMap>
#include <QMapIterator>
#include <QString>
int main()
{
using InnerMap = QMap<int, QString>;
using OuterMap = QMap<int, InnerMap>;
const OuterMap outerMap
{
{ 1, {{ 11, "a" }, { 12, "aa" }} },
{ 2, {{ 22, "b" }, { 21, "bb" }} },
{ 3, {{ 22, "c" }} }
};
qDebug() << "--- foreach (QMap) ---";
foreach ( const auto& outerKey, outerMap.keys() )
{
qDebug() << outerKey;
const auto& innerMap = outerMap[ outerKey ];
foreach ( const auto& innerKey, innerMap.keys() )
{
const auto& innerValue = innerMap[ innerKey ];
qDebug() << "\t{" << innerKey << "," << innerValue << "}";
}
}
qDebug() << "\n--- range-for (QMap -> std::map) ---";
const auto& m1 = outerMap.toStdMap();
for ( const auto& p1 : m1 )
{
qDebug() << p1.first;
const auto& m2 = p1.second.toStdMap();
for ( const auto& p2 : m2 )
{
qDebug() << "\t{" << p2.first << "," << p2.second << "}";
}
}
qDebug() << "\n--- while (QMapIterator) ---";
QMapIterator<int, InnerMap> outerIt{ outerMap };
while ( outerIt.hasNext() )
{
outerIt.next();
qDebug() << outerIt.key();
QMapIterator<int, QString> innerIt{ outerIt.value() };
while ( innerIt.hasNext() )
{
innerIt.next();
qDebug() << "\t{" << innerIt.key() << "," << innerIt.value() << "}";
}
}
return 0;
}
输出:
--- foreach (QMap) ---
1
{ 11 , "a" }
{ 12 , "aa" }
2
{ 21 , "bb" }
{ 22 , "b" }
3
{ 22 , "c" }
--- range-for (QMap -> std::map) ---
1
{ 11 , "a" }
{ 12 , "aa" }
2
{ 21 , "bb" }
{ 22 , "b" }
3
{ 22 , "c" }
--- while (QMapIterator) ---
1
{ 11 , "a" }
{ 12 , "aa" }
2
{ 21 , "bb" }
{ 22 , "b" }
3
{ 22 , "c" }
我会使用 QMapIterator Class。请参阅文档示例:
QMap<int, QWidget*> map;
// ...
QMapIterator<int, QWidget*> i(map);
while (i.hasNext()) {
i.next();
qDebug() << i.key() << ": " << i.value();
}