【发布时间】:2012-06-25 04:09:49
【问题描述】:
例如,假设您从某处提取数据并将其放入字符串变量中,然后您想使用其中的数据作为另一个字符串名称:
int main(void){
string strVar ="StringData"; //this is a string variable with text inside
cout<<strVar<<endl; //displaying the variables contents
string strVar.c_str() = "stuff in string variable 'StringData'"; //this uses what was inside of strVar to be the name of the new string variable
cout<<StringData<<endl; //prints the contents of the variable StringData (who got its name from the data inside of strVar
}
//OUTPUT:
StringData
stuff in string variable 'StringData'
我知道你绝对不能以这种方式做到这一点,在这个例子中,你必须在使用变量 StringData 之前事先知道 strVar 中的内容,但理论上我们可以这样做吗?
编辑:
谢谢大家,所以我从你们那里得到的基本上是不可能的,C++ 不是动态变量语言,我能得到的最接近它的是 地图(字符串,字符串)
【问题讨论】:
-
大概
map<string, string>是你想要的? -
简短的回答:不,不可能。
标签: c++ c variables declaration