如何在 for 循环中创建具有不同名称的多个对象

How to create multiple objects inside of for loop with different names?

本文关键字:对象 for 循环 创建      更新时间:2023-10-16

我有这个代码:

// Generate objects from type DepositoFresco or DepositoNormal
number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;
int j;
for (j = 0; j < number_depositosf; ++j) {
    type_deposito = 0;
    id_deposito = "df" + to_string(j);
    number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    list_depositos.push_back(new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance));
}

这段代码有效,但我想要的是创建具有不同名称的对象(具体而言,名称在"id_deposito"变量中)。我尝试做这样的事情:

number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;
int j;
for (j = 0; j < number_depositosf; ++j) {
    type_deposito = 0;
    id_deposito = "df" + to_string(j);
    number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    DepositoFresco id_deposito = new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance)
    list_depositos.push_back(id_deposito);
}

但它不起作用。有人知道如何解决它吗?

c++ 中无法从字符串创建或修改变量名称,但有一些解决方法。在这种情况下,可能效果更好的是哈希表。哈希表是一种数据结构,用于在对象之间创建单向关联,因此,如果您有对象 O1,则可以轻松检索之前保存的另一个 O2。在本例中,您希望使用字符串来访问 DepositoFresco 对象。

首先,您需要包括:

#include <map>

然后像这样创建你的哈希表:

std::map<std::string, DepositoFresco*> list_depositos;

保存如下阅读:

list_depositos[id_deposito] = new DepositoFresco(...);
list_depositos[id_deposito]

希望对您有所帮助! :D

#include <random>
#include <string>
#include <iostream>
#include <sstream>
class Deposito
{
private:
    int type_deposito;
public:
    Deposito(int type_deposito):
        type_deposito(type_deposito)
    {}
    void testFunc()
    {
        std::cout << "You called a deposito testFunc with type: " << this->type_deposito << std::endl;
    }
};
class DepositoFresco : public Deposito
{
private:
    std::string id_deposito;
    int number_paletes;
    int number_produtos;
    int capacity;
    int area;
    int distance;
public:
    DepositoFresco(int type_deposito, std::string id_deposito, int number_paletes, int number_produtos, int capacity, int area, int distance):
        Deposito(type_deposito),
        id_deposito(id_deposito),
        number_paletes(number_paletes),
        number_produtos(number_produtos),
        capacity(capacity),
        area(area),
        distance(distance)
    {}
    void testFunc()
    {
        std::cout << "You called a depositoFresco testFunc with id: " << this->id_deposito << std::endl;
    }
};
int main(int argc, char* argv[])
{
    int valormaximo = 100;
    int valorminimo = 0;
    int number_depositosf = rand() % (valormaximo - valorminimo + 1) + valorminimo;
    std::vector<Deposito*> list_depositos;
    int j;
    for (j = 0; j < number_depositosf; ++j) {
        int type_deposito = 0;
        std::stringstream ss;
        ss << j;
        std::string numStr(ss.str());
        std::string id_deposito = "df" + numStr;
        int number_paletes = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int number_produtos = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int capacity = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int area = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        int distance = rand() % (valormaximo - valorminimo + 1) + valorminimo;
        DepositoFresco* deposito = new DepositoFresco(type_deposito, id_deposito, number_paletes, number_produtos, capacity, area, distance);
        list_depositos.push_back(deposito);
    }
    if(list_depositos.size() > 1)
    {
        //Retrieve and cast
        DepositoFresco* retrieved_depositofresco = static_cast<DepositoFresco*>(list_depositos[0]);
        retrieved_depositofresco->testFunc();//Calls DepositoFresco::testFunc();
        //Simple retrieve
        Deposito* retrieved_deposito = list_depositos[0];
        retrieved_deposito->testFunc(); //Calls Deposito::testFunc()
    }
}

这应该编译和工作。注意:我制作的存款和存款壁画课程可能与您的不同。我正在根据我在您的代码中看到的内容做出假设。根据您的需要对其进行修改。

这将构建一个 Deposito* 的列表(C++向量),就像您展示您想要的那样。请注意我给你的检索小例子。我不得不static_cast才能调用 DepositoFresco 函数。否则,您将只能访问存款信息。

现在,如果您想根据deposito_id快速访问这些内容,您将需要一个哈希结构,在这种情况下,您将使用 std::map。有关地图示例,请参阅 SlySherZ。我不会为此费心包括代码。