【问题标题】:Vector Map Iteration forward / reverse向量图迭代正向/反向
【发布时间】:2010-11-29 18:52:54
【问题描述】:

我正在迭代的向量中有一组有序的键/值映射。我知道一个唯一键,基于此,我需要在它之前和之后获取键/值对,直到达到限制。例如:

QVector < QMap < QString, QString > > map;
QMap < QString, QString > temp;

temp.insert("key1", "parent");
map.append(temp);
temp.clear();
temp.insert("key2", "value1");
map.append(temp);
temp.clear();
temp.insert("key3", "value2");
map.append(temp);
temp.clear();
temp.insert("key4", "value3");
map.append(temp);
temp.clear();
temp.insert("key5", "parent");
map.append(temp);

假设我有一个值“key3”,我想在“key3”之前获取键/值对,直到达到“parent”,然后在它之后直到达到“parent”(不包括“parent”) , 我该怎么做呢?我想不出一个简单的方法。

QMap < QString, QString > newMap;

QMap < QString, QString >::iterator i;
for (int i = 0; i < map.size(); ++i) {

     QMap < QString, QString > vectorMap = map.at(i);
     for (j = vectorMap.begin(); j != vectorMap.end(); ++j) {

          if (j.key() == "key3") {

               //set a bool to true and
               //get j.key() and j.value() before and after until "parent" is reached
               newMap.insert(j.key(), j.value());

          }
     }
}

newMap 会有键/值对(“key2/value1”、“key3/value2”、“key4/value3”);

针对我现在正在尝试的内容进行了编辑:

QString selection = "key3";

 //for going backwards, still need to go forward
 for (int i = 0; i < map.size(); ++i) {

    QMap < QString, QString > vectorMap = map.at(i);
    QMapIterator < QString, QString > iter(vectorMap);

    while (iter.hasNext()) {

        iter.next();

        if (iter.key() == selection &&  iter.value() != "parent") {

            do {

                previousText = iter.previous();

                tempMap.insert(iter.key(), iter.value());
                newMap.append(tempMap);
                tempMap.clear();

                qDebug() << "previousText" << previousText;
                iter.previous();

            } while (previousText != "parent");

        }

    }

 }

【问题讨论】:

  • “我想在 'key3' 之前获取键/值 paris 直到达到 'parent',然后在它之后直到达到 'parent'',我该怎么做?”
  • 好的,我想我对你真正想要做什么有了更好的了解。给定一些键,您希望将每个QMap 存储在您的QVector&lt; QMap &gt; 中,并在该QMap 中的“父”值之间插入所有内容,前提是该映射段包含您的键。
  • 是的!我想不出一个体面的方法来做到这一点。我上面的编辑代码抛出一个错误,我仍然不确定它是最简单的解决方案。
  • @user375566,我已经更新了我的代码来做你想做的事,但它是高度未经测试的,因为我不再使用 C++/Qt 工作。评论它,这样你就知道我至少想表达什么。
  • QVector 中的每个 QMap 是否总是只包含 1 个键值对?如果是这样,为什么不直接使用 QMap 呢?

标签: c++ qt vector qt4 map


【解决方案1】:

怎么样:使用地图的“查找”函数(或其他)来获取指向您要查找的项目的迭代器(“key3”)。从那个“key3”迭代器构造一个反向迭代器。在两个单独的循环中,推进这两个迭代器中的每一个,直到它指向的包含“父级”。在每个循环中,随时收集元素。

【讨论】:

    【解决方案2】:

    edit 既然我想我已经理解了这个问题,那么我想到了一个解决方案。

    请注意,这利用了QMap 的一些特性,即当您遍历QMap 时,它会按键顺序遍历。它不处理您的QMap 具有多个相等键的情况。

    目前还没有经过高度测试,而且相当 hack-ish,但我希望它应该能让你克服当前的障碍。

    我试图让它足够干净,让你理解逻辑,但你可以单独使用QMap::iterator 来回移动以确定要复制的范围。

    QVector < QMap < QString, QString > > map;
    QMap < QString, QString > temp;
    
    // populate your maps...
    
    QMap < QString, QString > newMap;
    
    // traverse your QVector< QMap<QString, QString> > map...
    
    QString selection = "key3";
    
    QVectorIterator<QMap<QString, QString> > vecIter(map);
    while (vecIter.hasNext()) 
    {
         // pull out your QMap<QString, QString>    
         QMap<QString, QString> tempMap = vecIter.next();
    
         // see if selection exists in the map
         if (tempMap.find(selection) != tempMap.end())
         {
             // find list of keys that map to "parent"
             QList<QString> parentKeys = tempMap.keys("parent");
    
             // get the Key that maps to "parent" that precedes 'selection'
             QString preKey;
    
             QListIterator<QString> liter(parentKeys);
             while (liter.hasNext())
             {
                 if(liter.peekNext() < selection)
                 {
                     preKey = liter.next();
                 }
                 else
                 {
                     // we've passed the selection key, break the loop
                     break;
                 }
             }
    
             // now traverse the map and copy
             // QMap::upperBound returns an iterator to the next greatest key after the key you give it
             QMap<QString, QString>::const_iterator mit = tempMap.upperBound(preKey);
             for(; mit != tempMap.constEnd(); ++mit)
             {
                 if ((*mit)->value() != "parent"))
                 {
                     newMap.insert((*mit)->key(), (*mit)->value());
                 }
                 else
                 {
                     // break out because we've just encountered the "parent" value after our selection
                     break;
                 }
             }
         }
    } // while (vecIter.hasNext())
    

    【讨论】:

    • 不,地图应该包含“key3”,我无法让它工作。我编辑了代码以显示我正在尝试的内容,但出现错误。
    猜你喜欢
    • 1970-01-01
    • 2016-11-09
    • 2014-09-15
    • 2020-11-06
    • 2011-12-10
    • 1970-01-01
    • 2017-08-29
    • 2018-03-20
    • 2011-01-03
    相关资源
    最近更新 更多