生成变量名称 [英] Generating Variables' Names
本文介绍了生成变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用for命令生成一些变量。请查看以下代码:
I want to generate some variables using for command. look at code below:
for (char ch='a'; ch<='z'; ch++)
int ch=0;
这只是一个例子,运行上面的代码后,我想有int a,int b,int c ...
It just an example, after running code above, I want to have int a, int b, int c ...
另一个例子:
for (int i=0; i<10; i++)
int NewiEnd=0;
例如在上面运行代码之后,我们将有int New1End,int New2End等。
For example after running code above, we will have int New1End, int New2End etc.
希望我很清楚,我怎么能在C ++中做这样的事情
Hope I'm clear enough, How can I do such thing in C++??
推荐答案
不,不可能,不完全。但是,这是可能的:
No, not possible, not exactly. However, this is possible:
std::map<char,int> vars;
for (char ch='a'; ch<='z'; ch++)
vars[ch] = 0;
std::cout << vars['a'] << vars['b'] << vars['c'];
您也可以使用 std :: map< std :: string,int> ;
。
std::map<std::string,int> vars;
for (int i=0; i<10; i++)
vars["New" + std::to_string(i) + "End"] = 0;
std::cout << vars["New5End"];
这篇关于生成变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文