C++ 使用循环更改变量名

C++ 使用循环更改变量名,c++,C++,有没有一种不使用数组的方法可以通过循环写入以下内容: 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<<**????**<< 我想将这些表达式添加到一个名为c的数组中,该数组将作为cplex中模型的

有没有一种不使用数组的方法可以通过循环写入以下内容:

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<<**????**<<
我想将这些表达式添加到一个名为c的数组中,该数组将作为cplex中模型的输入。 然后在我从Cplex得到一个结果后,我想添加一个表达式c(I)并再次求解它。。。 直到我得到我想要的值。。。 IloExprArray也可以使用,但我不知道如何使用此方法添加表达式:

for(i= 0,...)
{
    c7 +=x[i];
}
int*varArray[]={&c1,&c2,&c3};
int size=sizeof(varArray)/sizeof(int*);
对于(int i=0;iSTD::CUT< P>如果我理解正确,你正在尝试动态地创建变量名。AcIk这是不可能的C++。

< P>我建议使用一个数组来做这个。你不应该用编译语言玩动态变量名。< /P>
int c[] = {2, 5, 7, 9, 3, 4, 6, 5};
for (int i = 0; i < 8; i++) cout // and so on...
intc[]={2,5,7,9,3,4,6,5};
对于(inti=0;i<8;i++)cout//等等。。。

如果你真的不能使用数组,为什么不尝试使用指针呢。我知道下面的示例本质上是一个数组,但是它有点不同,所以值得一试

#include <iostream>
#include <conio.h>

#define N 10

using namespace std;

int main() {

    int * a = new int[N];

    for (int i = 0, * b = a; i < N; i++, b++) {
        *b = i;    
    }


    for (int i = 0, * b = a; i < N; i++, b++) {
        cout << *b << endl;
    }

    getch();
}
#包括
#包括
#定义n10
使用名称空间std;
int main(){
int*a=新的int[N];
对于(inti=0,*b=a;i我一直都知道,如果“variable variablenames”是你问错问题的原因。它可以以不同的方式解决。

如果你想按词汇做任何事情,你就必须使用宏。请理解这很糟糕而且无法维护,但如果你坚持,你可以这样做:

#define Q(a) a
#define DO_THING(a, expr) do { Q(a)1 expr; Q(a)2 expr; Q(a)3 expr; Q(a)4 expr } while (0)

DO_THING(b, [i] = i);
这将导致代码:

do { b1  [i] = i; b2  [i] = i; b3  [i] = i; b4  [i] = i } while (0);

写了所有这些之后,请不要这样做。即使你用更多的宏来包装它,使其具有准可读性,请让常识占上风,放弃这个计划。

利用或元编程是否算数

#include <iostream>
template<unsigned U>
struct Fac{ enum { value = U * Fac<U-1>::value};};
template<>
struct Fac<0>{ enum { value = 1};};
template<unsigned U>
struct Fib{ enum {value = (Fib<U-1>::value + Fib<U-2>::value)};};
template<>
struct Fib<0>{ enum {value = 0};};
template<>
struct Fib<1>{ enum {value = 1};};
template<unsigned U>
void show(){
    show<U-1>();
    std::cout << "Fib(" << U << ")=" << Fib<U>::value << "\t" << "Fac(" << U << ")=" << Fac<U>::value << std::endl;
}
template<>
void show<0>(){}

int main(int argc, char** argv){
    show<12>();
}
#包括
模板
结构Fac{enum{value=U*Fac::value};};
模板
结构Fac{enum{value=1};};
模板
结构Fib{enum{value=(Fib::value+Fib::value)};};
模板
结构Fib{enum{value=0};};
模板
结构Fib{enum{value=1};};
模板
无效显示(){
show();

std::cout下面是在c/c++中使用预处理器操作符和宏实现这一点的方法 此链接上有一些使用示例

我使用过此功能,它工作正常:

 IloNumVarArray x(env, 10);

 for(i=0; i<10; i++){
   x[i] = IloNumVar(env, 0, IloInfinity); 

   // Code for change the name of each variable
   char index[2]; 
   sprintf(index, "%d", i); 
   char variable_name[6] = "x"; 
   strcat(variable_name, index); 
   x[i].setName(variable_name);
 }
IloNumVarArray x(env,10);

对于(i=0;i为什么不使用数组?如果变量形成一个集合,数组(或其他可枚举集合)似乎是正确的解决方案。永远不需要“更改变量名”在C++中,因为你可以简单地创建你喜欢的任何变量的指针。为什么不使用文本处理器或者代码> Perl < /Cube >生成你的源代码?因为链接指针,因为你分配了新的指针,变量也动态地被动态地填充。我很害怕。谢谢。e循环但这有点可笑也许我遗漏了什么,但为什么循环到7?我的错误。它应该是数组的长度。
#include <iostream>
template<unsigned U>
struct Fac{ enum { value = U * Fac<U-1>::value};};
template<>
struct Fac<0>{ enum { value = 1};};
template<unsigned U>
struct Fib{ enum {value = (Fib<U-1>::value + Fib<U-2>::value)};};
template<>
struct Fib<0>{ enum {value = 0};};
template<>
struct Fib<1>{ enum {value = 1};};
template<unsigned U>
void show(){
    show<U-1>();
    std::cout << "Fib(" << U << ")=" << Fib<U>::value << "\t" << "Fac(" << U << ")=" << Fac<U>::value << std::endl;
}
template<>
void show<0>(){}

int main(int argc, char** argv){
    show<12>();
}
 IloNumVarArray x(env, 10);

 for(i=0; i<10; i++){
   x[i] = IloNumVar(env, 0, IloInfinity); 

   // Code for change the name of each variable
   char index[2]; 
   sprintf(index, "%d", i); 
   char variable_name[6] = "x"; 
   strcat(variable_name, index); 
   x[i].setName(variable_name);
 }