在CPP / c。使用字符串值数据中声明变量名称++ [英] Declare variables names using string value data in cpp/c++

查看:388
本文介绍了在CPP / c。使用字符串值数据中声明变量名称++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比如可以说你从什么地方获取数据,你把它放在一个字符串变量,那么你要使用它里面的数据是另一个字符串名称:

for example lets say your pulling data from somewhere and you put it in a string variable then you want to use the data inside of it to be the another strings name:

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'

我知道你绝对不能做这种方式,并在这个例子中你会有什么样的手在你strVar使用变量StringData是前前需要了解的,但我们可以从理论上做到这一点?

i know you definitely cannot do it in this manner and in this example your would have to know before hand what was in strVar before you used the variable StringData, but can we theoretically do this?

编辑:

谢谢大家,所以我想从你全部搞定,基本上它的不可能的,C ++是不是一个动态变量的语言,我可以得到它最接近是一个
    图(字符串,字符串)

Thanks everyone, so what i get from you all is basically its not possible, C++ is not a Dynamic variable language and the closest thing i can get to it is with a map (string, string)

推荐答案

没有明确像你所想的,但也许你会有兴趣在的std ::地图 ?听起来像是你想要的是一个key-value配对。

Nothing explicitly like you are thinking, but maybe you'd be interested in a std::map? Sounds like what you want is a Key-Value pairing.

参考的std ::地图 - >
http://www.cplusplus.com/reference/stl/map/

Reference for std::Map -> http://www.cplusplus.com/reference/stl/map/

例如:

#include <map>
#include <string>
#include <iostream>
using namespace std;

int main(void)
{
    map<string, string> myMap;
    myMap.insert(pair<string, string>("StringData", "Stuff in StringData"));

    // Get our data
    cout << myMap["StringData"] << endl;

    return 0;
}

这篇关于在CPP / c。使用字符串值数据中声明变量名称++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆