【问题标题】:QHash removal of specified position till the end of QHash and update the QList accordinglyQHash 移除指定位置直到 QHash 结束并相应地更新 QList
【发布时间】:2018-09-17 06:52:17
【问题描述】:

我有一个函数,一旦应用就会从给定位置拆分 QHash 直到结束,并且需要相应地更新随附的 QList 以反映 QHash 的变化。 QHash 存储为QHash<QString(Train ID), int(position of train)> 以便快速查找。 QList 将 QList 存储为指向对象的指针列表。此函数的给定标头是:QList<FreightCar*> splitTrain(int pos) 并且不能更改(即只能在标头中按原样使用)。 我已经获得了仅删除给定值“pos”的功能,并且不会通过带有迭代器的 For 循环遍历 QHash。在 QHash 之后,QList 被更新为 QHash 中的值,但不完全是。

这里是函数的实现。

QList<FreightCar*> FreightTrain::splitTrain(int pos)
{
    QTextStream cout(stdout);
    QHash<QString, int>::iterator i;
    QList<FreightCar*>::iterator j;

int posI;
posI = pos;
//removal of posbyid by given id onwards
        for(i = this->posByID.begin(); i != this->posByID.end(); )
        {
                  if(i.value() == posI)
                  {
                      i = this->posByID.erase(i++);
                      posI++;
                  }
                  else
                     i++;

         }
        //end of removal of posbyid by given id onwards

        //Display current values in QHash of posByID
        for(auto i = this->posByID.begin(); i != this->posByID.end(); i++)
        {
            cout << "keyid: " << i.key() << " valpos: " << i.value() << endl;
        };
        //End of display of posByID values




//removal of same ID values from QHASH posbyId in QLIST physicalorder
bool yes;
        for (auto j = this->physicalOrder.begin(); j != this->physicalOrder.end();)
        {
            yes = false;

                   for(auto i = this->posByID.begin(); i != this->posByID.end(); ++i)
                   {
                        if((*j)->getID() == i.key())
                        {
                            if(i.key() == pos)
                            {
                                yes = true;
                                break;
                            }else
                            {
                                yes = false;
                                break;
                            }
                        }else if((*j)->getID() != i.key())
                        {
                            yes = true;
                        }
                   }
                if(yes == true)
                {
                  j= this->physicalOrder.erase(j);
                }else
                {j++;};
            }//end of removal of same ID values from posbyId in physicalorder

//display of QList after deletion.
        for (auto j = this->physicalOrder.begin(); j != this->physicalOrder.end(); j++)
        {
            cout << (*j)->toString() << endl;
        }
//end of display of list after deletion.

    return *this;

};

我创建了一些对象(火车车厢)后程序的输出:

拆分功能需要从给定位置删除所有值/对象直到结束。因此,需要保留从开始到提供位置的所有值。

货运列车中的每个对象(货车)都是按物理顺序创建的。每辆货车都有一个 ID,一个指定的重量(吨位)和指定的类型(BOXCAR、GONDOLACAR、TANKCAR、FLATBEDCAR)。 “valpos”是指每辆车的创建顺序 1,2,3,4,5 等。 “keyid”是指实物订单QList中货车的ID。

【问题讨论】:

  • 您的问题有几处不清楚。什么是火车ID?这真的能识别货车,而不是火车吗?类似地,“火车的位置”实际上是指“车在火车内的位置”吗?代码的主要问题似乎是 a) 假设 QHash 具有迭代顺序,或者对 QHash 的迭代是您想要做的事情,并且 b) 不清楚哪些代码负责清除什么;您有两个循环都修改了 QHash,但没有明确的理由为什么需要这样做。
  • 这个程序是关于Freight Trains的,每个FreightCar都有一个type、id、position和tonnage 当一个货车被添加时,它被放置在QList(physicalOrder)中作为名称的创建顺序状态。 QHash 用于快速查找列表中的汽车,以说明汽车在火车中的顺序是什么 ID。
  • @SebastianRedl ID 只是为了说明您所指的货运车,每辆车都是通过物理顺序(就像现实生活中的火车)一个接一个地放置的。由于您可以拥有 5 辆 FLATBEDCAR,因此您需要实物订单或 ID 来区分汽车。
  • @SebastianRedl 我可以在创建对象(货运车)期间制作 ID 的任何数字,还可以说明它的吨位以及它所属的汽车类型。在创建每个对象的过程中,汽车是按顺序放置的,这就是“valpos”所指的地方。

标签: c++ qt qt5


【解决方案1】:

通过复制/粘贴通过 QHash 运行的初始循环以再次通过循环运行以删除以下值来使其工作,因为 QHash 与 QMap 相同,因此值不与数组保存相同,因此您循环过去的某些值。

【讨论】:

    猜你喜欢
    • 2015-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多