1

新蜜蜂在这里。如果有类似的问题,我很抱歉,但我什至不知道如何正确提问。问题是,我必须为大学做一些项目,我现在被困住了。我通过 udp 获取信息,需要存储产量信息以传递给对象(汽车坐标),我必须为 20 个对象执行此操作,因此它要复杂得多。我有这个代码,它应该作为我的教授工作。告诉我,但我必须自己弄清楚如何存储它。试过 QList 和 QMap 但我不知道怎么可能

anArray[num].posX=somenumber;

所以我必须将每辆车的坐标存储为 car1.x、car1.y、car1z,然后在循环中增加;car2.x 等等。

我不知道我的问题或我想问的是否足够清楚,但请多多包涵

while(mSocket->hasPendingDatagrams()){
        QByteArray datagram;
        QHostAddress crrAddress;
        quint16 crrPort;
        datagram.resize( int(mSocket->pendingDatagramSize()) );
        mSocket->readDatagram(datagram.data(), datagram.size(), &crrAddress, &crrPort);             
        double* resultList = new double[(datagram.size() / int(sizeof(double))) ];

        memcpy(&resultList[0], datagram.data(), size_t(datagram.size()));

        for(quint16 count = 0; (count) < mVehicleCount; count++) {
            mVehicleMap[mArrayOffset + count].ID = int16_t(resultList[7 + 11 * count]);
            mVehicleMap[mArrayOffset + count].pose.position.x = resultList[(1 + 11 * count)];
            mVehicleMap[mArrayOffset + count].pose.position.y = resultList[(2 + 11 * count)];
            mVehicleMap[mArrayOffset + count].pose.position.z = resultList[(3 + 11 * count)];
            mVehicleMap[mArrayOffset + count].pose.orientation.x = resultList[(6 + 11 * count)];
        }
        delete[] resultList;
4

1 回答 1

1

您可以为您的目标使用QListQVector分类(Qt 文档:QListQVector)。

简单示例QVector

    struct Vehicle
    {
        qint16 ID_;
        double position_x_;
        double position_y_;
        double position_z_;
        double orintation_x_;
    };

    ...

    QVector<Vehicle> vehicles(maxVehicleCount);  // maxVehicalCount - you variable

    ...

    while(mSocket->hasPendingDatagrams()){
            QByteArray datagram;
            QHostAddress crrAddress;
            quint16 crrPort;
            datagram.resize( int(mSocket->pendingDatagramSize()) );
            mSocket->readDatagram(datagram.data(), datagram.size(), &crrAddress, &crrPort);             
            double* resultList = new double[(datagram.size() / int(sizeof(double))) ];

            memcpy(&resultList[0], datagram.data(), size_t(datagram.size()));

            for(quint16 count = 0; (count) < mVehicleCount; count++) {
                vehicles[mArrayOffset + count].ID_ = qint16(resultList[7 + 11 * count]);
                vehicles[mArrayOffset + count].position_x_ = resultList[(1 + 11 * count)];
                vehicles[mArrayOffset + count].position_y_ = resultList[(2 + 11 * count)];
                vehicles[mArrayOffset + count].position_z_ = resultList[(3 + 11 * count)];
                vehicles[mArrayOffset + count].orintation_x_ = resultList[(6 + 11 * count)];
            }
            delete[] resultList;
    }

您也可以使用QMap类(Qt doucmentation:QMap):

    struct Vehicle
    {
        qint16 ID_;
        double position_x_;
        double position_y_;
        double position_z_;
        double orintation_x_;
    };

    ...

    QMap<qint16, Vehicle*> vehicleMap;

    ...

    while(true/*mSocket->hasPendingDatagrams()*/){
            QByteArray datagram;
            QHostAddress crrAddress;
            quint16 crrPort;
            datagram.resize( int(mSocket->pendingDatagramSize()) );
            mSocket->readDatagram(datagram.data(), datagram.size(), &crrAddress, &crrPort);             
            double* resultList = new double[(datagram.size() / int(sizeof(double))) ];

            memcpy(&resultList[0], datagram.data(), size_t(datagram.size()));

            for(quint16 count = 0; (count) < mVehicleCount; count++) {
                Vehicle* vehicle = new Vehicle();
                vehicle.ID_ = qint16(resultList[7 + 11 * count]);
                vehicle.position_x_ = resultList[(1 + 11 * count)];
                vehicle.position_y_ = resultList[(2 + 11 * count)];
                vehicle.position_z_ = resultList[(3 + 11 * count)];
                vehicle.orintation_x_ = resultList[(6 + 11 * count)];
                vehicleMap.insert(vehicle.ID_, vehicle);
            }
            delete[] resultList;
    }
于 2016-06-08T05:36:44.497 回答