【问题标题】:How to Iterate and Insert Items in Qmap?如何在 Qmap 中迭代和插入项目?
【发布时间】:2019-06-02 15:58:39
【问题描述】:

我有一个 Qmap,包含一些对象(它不是免费的),为了简单起见,假设我想将每个对象的子对象添加到 Qmap 中,所以我必须迭代 Qmap 并将每个对象的子对象插入其中.我该怎么做?然而,真正复杂一点的代码在这里:

// This parameterSearchList will be looped over to include the the parameters that are implicitly used in the function in the lists of function.
// This includes both parameter QProperties of random variables and decision variables and the parameters upstream.
// The type of the parameter is determined by down-casting a pointer of the types RRandomVariable, RDecisionVariable, RConstant and RResponse to the parameter, respectively.
// If the down-casting to -for example- RRandomVariable does not return a NULL pointer, then the type of this parameter is a random variable. Thus, the pointer is added to the list of random variables.
// Likewise, if the type of the parameter is determined to be RDecisionVarible, RConstant, or RResponse, again the pointer is added to the corresponding lists, which are theDecisionVariableList, theConstantList, and theResponseList.
QMap<QString,RParameter *> parameterSearchList = theExplicitParameterList;
int counter = 0;
QMap<QString, RParameter *>::iterator iter;
for (iter=parameterSearchList.begin(); iter != parameterSearchList.end(); ++iter) {
    if (counter++ % 100 == 0) {
        QCoreApplication::processEvents();
    }

    RParameter *parameterObject = iter.value();

    // adding a lock for the parameter if it has not been added before
    if (! theDependencyCalculationLockHash.contains(parameterObject))
        theDependencyCalculationLockHash.insert(parameterObject, new QReadWriteLock());

    // Down-casting a pointer with the type RRandomVariable to the parameter
    RRandomVariable *randomVariableObject = qobject_cast<RRandomVariable *>(parameterObject);
    // If the pointer is not NULL, then the type of the parameter is random variable. Thus, this parameter should be added to the list of random variables.
    if (randomVariableObject) {
        // If "theRandomVariableList" does not already contain this parameter ... 
        if (!theRandomVariableList.contains(randomVariableObject)) {
            // Adding the parameter to the "theRandomVariableList"
            theRandomVariableList.append(randomVariableObject);

            // Adding the parameter QProperties of the random variable to the parameterSearchList
            QList<RParameter *> tempParameterList = randomVariableObject->getParameterList();
            for (int j = 0; j < tempParameterList.count(); j++) {
                if (!parameterSearchList.contains(tempParameterList[j]->objectName())) {
                    parameterSearchList.insert(tempParameterList[j]->objectName(),tempParameterList[j]);
                }
            }
        }
        continue;
    }

    // Down-casting a pointer with the type RDecisionVariable to the parameter
    RDecisionVariable *decisionVariableObject = qobject_cast<RDecisionVariable *>(parameterObject);
    // If the pointer is not NULL, then the type of the parameter is decision variable. Thus, this parameter should be added to the list of decision variables.
    if (decisionVariableObject) {
        // If "theDecisionVariableList" does not already contain this parameter ... 
        if (!theDecisionVariableList.contains(decisionVariableObject)) {
            // Adding the parameter to the "theDecisionVariableList"
            theDecisionVariableList.append(decisionVariableObject);

            // Adding the parameter QProperties of the decision variable to the parameterSearchList
            QList<RParameter *> tempParameterList = decisionVariableObject->getParameterList();
            for (int j = 0; j < tempParameterList.count(); j++) {
                if (!parameterSearchList.contains(tempParameterList[j]->objectName())) {
                    parameterSearchList.insert(tempParameterList[j]->objectName(), tempParameterList[j]);
                }
            }
        }
        continue;
    }

    // Down-casting a pointer with the type RConstant to the parameter
    RConstant *constantObject = qobject_cast<RConstant *>(parameterObject);
    // If the pointer is not NULL, then the type of the parameter is constant. Thus, this parameter should be added to the list of constants.
    if (constantObject) {
        // If "theConstantList" does not already contain this parameter ... 
        if (!theConstantList.contains(constantObject)) {
            // Adding the parameter to the "theConstantList"
            theConstantList.append(constantObject);
        }
        continue;
    }
}

【问题讨论】:

  • 您能否根据您的代码简化您的问题:您会将其孩子添加到您的代码中的哪个objects
  • theExplicitParameterList 中有一些objects,我将它们复制到一个名为parameterSearchList 的新地图中。现在我想迭代它并将parameterSearchListobjects 的孩子添加到parameterSearchList!如您所知,问题是可能在迭代器之前添加了一些子代,因此迭代器看不到它们,我也找不到它们的子代,(这就像一个递归迭代,在此之前我使用了Qlist 并且因为新添加了子代Qlist我能够迭代它们并找到它们的孩子)

标签: qt qmap


【解决方案1】:

这就是你要找的东西:

  • 不要直接迭代地图,获取并存储您的QMap 的键列表QList&lt;String&gt; 并按如下方式迭代此列表:
// Get the list of maps keys
QList<QString> keys = parameterSearchList.keys();

// iterate keys
for(QString p : keys){

    // Down-casting a pointer with the type RRandomVariable to the parameter
    RRandomVariable *randomVariableObject = qobject_cast<RRandomVariable *>(parameterSearchList[p]);

    // If the pointer is not NULL, then the type of the parameter is random variable. Thus, this parameter should be added to the list of random variables.
    if (randomVariableObject) {

        // Iterate ParameterList
        for(RParameter * param : randomVariableObject->getParameterList()) {

            // Check if not already exist in parameterSearchList
            // (optional if your sure that parameters are unique)
            if(!parameterSearchList.contains(param->objectName())){

                // add param to parameterSearchList
                parameterSearchList.insert(param->objectName(), param);

            }
        }

    }
}

希望对你有帮助。

【讨论】:

  • 我想使用QMap,因为与QList相比,.contains() 的时间复杂度更低。但是,我将我的QList 复制到QMap,然后我在Qlist 上进行迭代,但在QMap 上检查.contains()
  • 在我的回答中,contains() 仅用于QMap。所以,抱歉,我没明白你的意思。
  • 你说得对,我只是不想使用临时的QList,这似乎是不可能的。
  • 我认为这是做你想做的最好的方法,但我会尝试考虑另一种解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-02
  • 2021-12-29
  • 1970-01-01
  • 2022-10-24
相关资源
最近更新 更多