如何重命名QMap的Key?
How to rename Key for QMap?
我有这个QMap<int, myDB> myTable
像这样
(1, {something0})
(3, {something1})
(5, {something2})
(8, {something3})
如何更改 myTable
的第一个密钥从 0 开始的密钥?
(0, {something0})
(1, {something1})
(2, {something2})
(3, {something3})
不能直接在原始地图中更改键,而是创建另一个地图,将值分配给具有重组键的另一个地图,然后使用QMap::swap
替换原始地图项。片段看起来像
//Make sure myDB is assignable
QMap<int, myDB> other;
QList<int> keys = myTable.uniqueKeys();
for (int k = 0; k < keys.length(); k++) {
int key = keys[k];
//We're using insertMulti in case
//we have multiple values associated to single key
QList<myDB> values = myTable.values(key);
for (int j = 0; j < values.length(); j++) {
other.insertMulti(k, values.at(j));
}
}
myTable.swap(other);
我有这个QMap<int, myDB> myTable
像这样
(1, {something0})
(3, {something1})
(5, {something2})
(8, {something3})
如何更改 myTable
的第一个密钥从 0 开始的密钥?
(0, {something0})
(1, {something1})
(2, {something2})
(3, {something3})
不能直接在原始地图中更改键,而是创建另一个地图,将值分配给具有重组键的另一个地图,然后使用QMap::swap
替换原始地图项。片段看起来像
//Make sure myDB is assignable
QMap<int, myDB> other;
QList<int> keys = myTable.uniqueKeys();
for (int k = 0; k < keys.length(); k++) {
int key = keys[k];
//We're using insertMulti in case
//we have multiple values associated to single key
QList<myDB> values = myTable.values(key);
for (int j = 0; j < values.length(); j++) {
other.insertMulti(k, values.at(j));
}
}
myTable.swap(other);