类类型的多维向量

时间:2013-04-23 18:45:58

标签: c++ vector

当类型为类时,我需要一些矢量向量的帮助。 “loadTruck”是我的班级。我想按照以下顺序将我的类“loadTruck”中的值存储到2D矢量中:vector(truck,print)。 “truck”和“print”都是类“loadTruck”的类型。我想访问这个向量:例如,假设“print”是包,“truck”是卡车。我想获得有关包裹在卡车1中的信息,包裹号为20.我完全对此感到困惑。

vector<vector <loadTruck>> jedinecTruck;
loadTruck truck;

while (warehouseIsEmpty == False)
{
    truck.resetTruck();
    for (int i = 0; i < numOfPackages; i++)
    {
        vector<loadTruck> print;
        if (store2D[i].checkIfPackageIsThere() == true)
        {
            truck.setPackageSize(store2D[i].getPackageWidth(), store2D[i].getPackageHeight());
            if (truck.checkIfPackageCanBeLoaded() == true)
            {
                print.push_back(truck);
                truck.loadPackageInTruck();
                store2D[i].deletePackage();
            }
        }
    }
    jedinecTruck.push_back(truck);

}

带3D阵列的版本,\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ 现在programm运行良好,但数组并不是一个好主意,但我希望它能帮助你理解我想做什么。我需要使用向量,因为我无法定义数组的前2个元素,它依赖于algorythm并且总是在变化。对于这个例子,我假设前两个元素将被静态定义。第3个元素总是有4个组件。

    vector<loadTruck> jedinecTruck;
loadTruck truck;

int array[1][3][4];

while (wharehouseIsEmpty == false)
{

    truck.resetTruck();
    for (int i = 0; i < numOfPackages; i++)
    {
        if (store2D[i].checkIfPackageIsThere() == true)
        {
            truck.setPackageSize(store2D[i].getPackageWidth(), store2D[i].getPackageHeight());
            if (truck.checkIfPackageCanBeLoaded() == true)
            {
                truck.loadPackageInTruck();
                store2D[i].deletePackage();
                array[0][i][0] = truck.getActualX1();
                array[0][i][1] = truck.getActualX2();
                array[0][i][2] = truck.getActualY1();
                array[0][i][3] = truck.getActualY2();
            }
        }
    }
    jedinecTruck.push_back(truck);
  }

此致

1 个答案:

答案 0 :(得分:0)

也许这更接近你想要做的事情:

vector<vector <loadTruck>> jedinecTruck;
loadTruck truck;

while (warehouseIsEmpty == False)
{
    truck.resetTruck();
    vector<loadTruck> print;
    for (int i = 0; i < numOfPackages; i++)
    {
        if (store2D[i].checkIfPackageIsThere() == true)
        {
            truck.setPackageSize(store2D[i].getPackageWidth(), store2D[i].getPackageHeight());
            if (truck.checkIfPackageCanBeLoaded() == true)
            {
                print.push_back(truck);
                truck.loadPackageInTruck();
                store2D[i].deletePackage();
            }
        }
    }
    jedinecTruck.push_back(print);

}

但是很难从你的问题中看出来。