如何在C ++中使用字符串作为变量名?

如何在C ++中使用字符串作为变量名?

问题描述:


可能重复:

将字符串转换为变量名称或变量类型

如何在c ++中使用字符串值作为变量名

How to use the string value as a variable name in c++

string listName = "hari";
string vectorName = "BF_vector_"+listName;
vector<string> vectorName;

vectorName.push_back("Some Value");

如何使用vectorName的字符串值(BF_vector_hari)作为向量的变量名。
提前感谢。

How to use the string value("BF_vector_hari") of vectorName as a variable name of vector.? Thanks in advance.

不能在C ++中。

你可以做的是使用 std :: map< std :: string,std :: vector> 的形式存储名称到向量图。

One thing you can do is use a form of std::map<std::string, std::vector> to store name to vector map.