如何在 for 循环中创建具有不同名称的多个对象
How to create multiple objects inside of for loop with different names?
我有这个代码:
// 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。我不会为此费心包括代码。
相关文章:
- C++:返回一个基于范围 for 循环迭代器,其中包含继承对象
- Pybind11:使用 for 循环使用 OpenMP 访问 python 对象
- 如何在 c++ 中使用 for 循环输出矢量中的所有对象
- 我想在 C++ 中使用 for 循环创建类对象
- 基于范围的 for 循环将对象移动到另一个容器中?
- 访问类的 std::vector 与 for 循环中的 custum 类对象
- 非常量对象的向量似乎在基于范围的 for 循环中被视为常量
- 调用模板函数的问题"No matching function for call"参数:迭代器、对象函数
- 基于范围的 for 循环,用于包含C++中的指针的自定义链表,仅返回对象
- 在<Object>基于范围的 for 循环中从 std::vector 获取指向对象的指针
- 在 for 循环中创建的对象具有相同的地址
- 当从此到子类中的新对象时,将显示警告"destination for this 'memcpy' call is a pointer to dynamic class..."
- C 试图初始化我在for循环中写的类的对象
- 基于范围的 for 循环的对象生存期问题
- 为什么OpenCV Mat对象在嵌套for循环中指定了预期值后却不包含这些值
- 如何在每次运行循环时更改for循环中的对象
- 获取 while、if 和 for 语句的用户定义类的对象值
- 对象, 在 Vector 中, 在'For Loop'初始化中损坏
- 对象初始化和循环在"for"中?
- 以旧方式C++遍历对象指针数组(没有基于范围的 for)