相关疑难解决方法(0)

用循环更改变量名称

有没有办法不使用数组用循环编写以下内容:

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<<**????**<<
Run Code Online (Sandbox Code Playgroud)

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

编辑:非常感谢所有的输入,这里有一些代码来帮助说明我的问题...我使用C++与C++.循环不会在7结束,但是当满足停止标准时

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);
}
Run Code Online (Sandbox Code Playgroud)

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

for(i= 0,...)
{
    c7 +=x[i];
}
Run Code Online (Sandbox Code Playgroud)

c++

5
推荐指数
3
解决办法
2万
查看次数

标签 统计

c++ ×1