使用循环更改变量名称 [英] change variable name with a loop

查看:143
本文介绍了使用循环更改变量名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用数组来写下面一个循环的方法:

Is there a way without using arrays to write the following with a loop:

cout<<"This variable c1 ="c1
cout<<"This variable c2 ="c2
cout<<"This variable c3 ="c3

for(i=1,i<8,i++)
cout<<"This variable c%d =",i<<**????**<<

这显然不是我需要做的,但是我可以想到的最简单的例子同样的问题...
所以我想做的是更改循环中的变量,而不是输出!

This is obviously not what I Need to be done but is the easiest example I could think of with the same problem... So what I would like to do is change the variables in the loop, not the output!

编辑:
很多为所有的输入,
这里是一些更多的代码来帮助说明我的问题... Im使用cplex与c ++。
当满足停止条件时,循环不会以7结束

Thanks a lot for all the input, here is a bit more of the code to help illustrate my problem...Im Using Cplex with c++. The loop will not end at seven but when a stop criteria is met

static void  populatebyrow (IloModel model, IloNumVarArray x, IloRangeArray c)
{
    IloExpr c1(env);
    IloExpr c2(env);
    IloExpr c3(env);
    IloExpr c4(env);

    c.add(c1>=n);
    c.add(c2>=n); ...

    model.add(c);
}



我想将这些表达式添加到名为c的数组对于cplex中的模型。
然后,我得到一个结果从Cplex我想添加一个表达式c(i)并再次解决...
这直到我得到我想要的值...
IloExprArray可以也可以用某种方式,但是我不知道如何添加表达式使用这种方法:

I Want to add these expressions to an Array called c that will be an input for a model in cplex. Then after I get a result from Cplex I want to add an expression c(i) and solve it again... This until i get the values I want... IloExprArray could also be used somehow, but then I dont know how to add the expressions using this method:

for(i= 0,...)
{
    c7 +=x[i];
}


推荐答案

正试图动态创建变量名。 AFAIK这是不可能与C ++。

If I understand correctly, you are trying to create variable names dynamically. AFAIK this is not possible with C++.

这篇关于使用循环更改变量名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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