2014-11-24 122 views
0

我有这样的代码:如何使用不同的名称在for循环中创建多个对象?

// 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”变量名)。我tryed做这样的事情:

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); 
} 

但它不工作。 任何人都知道如何解决它?

+1

什么类型是list_depositos?你发布的大部分代码对于你的要求都是不必要的。 – user1810087 2014-11-24 10:33:37

+0

我不知道你想要做什么,但有几个问题:'DepositoFresco id_deposito = new DepositoFresco'你指定一个非指针变量的指针。同样在第一个片段中,你可以使用'push_back()'指针,在第二个片段中你可以使用非指针。 – 2014-11-24 10:33:58

+0

我想你想要一个std :: map 。所以你可以做'list_depositos [id_deposito] = new DepositoFresco(...);' – LMF 2014-11-24 10:35:22

回答

0

没有办法从C++中的字符串创建或修改变量名,但有几个解决方法。 也许在这种情况下更好地工作的是散列表。散列表是一种在对象之间创建单向关联的数据结构,因此如果您有对象O1,则可以轻松地检索先前保存的另一个O2。 在这种情况下,您想使用字符串来访问DepositoFresco对象。

首先你需要包括:

#include <map> 

然后创建您的哈希表如下所示:

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

保存到它的读取,像这样:

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

希望它能帮助! :D

+0

我认为做#include 是你需要做的。 – 2014-11-24 11:53:57

+0

你说得对。可怕的Ctr + C; Ctr + V技能xD – SlySherZ 2014-11-24 11:56:23

0
#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和DepositoFresco课程。我根据我在代码中看到的内容做出了假设。修改它以满足您的需求。

这将构建Deposito *的列表(C++向量),就像您向您展示的那样。注意我给你提取的小例子。我必须static_cast才能调用DepositoFresco函数。如果你不这样做,你只能访问Deposito信息。

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

相关问题