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

大城市里の小女人 提交于 2019-12-13 04:14:00

问题


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 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.).

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


回答1:


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:


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.



来源:https://stackoverflow.com/questions/18125041/c-vector-declaring-multiple-variables-with-names-xxx1-xxx2-xxxn-n-loaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!