C++ vector: declaring multiple variables with names xxx1, xxx2…xxxN (N loaded from file)

时间:2022-09-24 22:12:12

I am up to upgrade a program to make it dynamically configurable from files. What i need is a number of vector viables, and that number being dependant of int variable.

我要升级程序,使其可以从文件中动态配置。我需要的是一些矢量元素,并且该数字依赖于int变量。

int k=4 //loaded from file, i handled it

vector<string> NAME(k)

Moreover, names of those variables need to be rising numbers (first object: NAME1, second NAME2 etc.).

而且,这些变量的名称需要是上升数字(第一个对象:NAME1,第二个NAME2等)。

This is my first ever post there, so sorry for all the mistakes or lack of information :)

这是我在那里的第一篇帖子,很抱歉所有的错误或缺乏信息:)

2 个解决方案

#1


2  

You can't dynamically name variables, but you could store them in a map.

您无法动态命名变量,但可以将它们存储在地图中。

std::map<std::string, std::vector<std::string> > myVectors;

for (int i = 0; i < k; ++i)
{
  std::ostringstream name;
  name << "NAME" << i;

  myVectors.insert(std::make_pair(name.str(), std::vector<std::string>()));
}

#2


0  

Use

使用

vector<vector<string> > name(k);

Where do you get the names from? I from file you could map instead. Probably yes when reading again sorry. Did not solve more than part of the problem.

你从哪里得到这些名字?我从文件中你可以映射。可能是的,再次阅读对不起。没有解决超过部分问题。

#1


2  

You can't dynamically name variables, but you could store them in a map.

您无法动态命名变量,但可以将它们存储在地图中。

std::map<std::string, std::vector<std::string> > myVectors;

for (int i = 0; i < k; ++i)
{
  std::ostringstream name;
  name << "NAME" << i;

  myVectors.insert(std::make_pair(name.str(), std::vector<std::string>()));
}

#2


0  

Use

使用

vector<vector<string> > name(k);

Where do you get the names from? I from file you could map instead. Probably yes when reading again sorry. Did not solve more than part of the problem.

你从哪里得到这些名字?我从文件中你可以映射。可能是的,再次阅读对不起。没有解决超过部分问题。