11

I'd like to use dynamic names if it is possible. Here an example about what I mean:

int sol1,sol2;
for(int i=1; i<3; i++){
   sol"i"=i*i;
   return max(sol1,sol2);
}

With sol"i" I mean sol1 in the first cycle (when i=1) and sol2 for the second (when i=2). Is this possibile in a similar way?

1
  • 3
    If you need to dynamically access a particular value, then a unique variable name - known at compile-time - is probably not the way you want to identify it. Commented Mar 25, 2015 at 13:49

5 Answers 5

14

It is not possible to do what you're asking, but there are alternatives that you should find equally expressive.

Probably the most common approach is to use a vector (or array) and index it:

std::vector<int> sol(2);
for (int i = 0; i < 2; ++i) {
    sol[i] = i * i;
}

Another approach is to use a std::map to map the desired name to the resulting variable:

std::map<std::string, int> variables;
for (int i = 1; i < 3; ++i) {
    std::string varname = "sol" + std::to_string(i);
    variables[varname] = i * i;
}

Note, however, that this is an extremely slow solution. I mention it only because it allows you to do something similar to your original example. Use the vector / array approach instead.

Sign up to request clarification or add additional context in comments.

2 Comments

You can use an std::unordered_map instead of a std::map where the operator[] has an average of constant time.
@NathanOliver: Thanks for mentioning this. I keep forgetting about std::unordered_map!
3

You are better off using an array.

int sol[2];
for(int i = 0; i < 2; i++)
{
  sol[i] = i*i;
}

return max(sol[0], sol[1]);

1 Comment

You are right, but please also answer the question.
3

This is not possible. You can use an array instead:

int sol[2];
for(int i=0; i<2; i++){
  sol[i]=i*i;
}
return max(sol[0],sol[1]);

Please note that in C++ arrays are 0-indexed.

Comments

1

As said by others, what you're asking about is impossible, but there are many alternative solutions. Using a C-array or an std::array of ints as suggested is the best approach if you can use it.

But supposed the variable names cannot be changes (for example, they are argument of a function, and you don't want to change the signature), but you still want to address them by index, so you can loop through them. One approach is to use an array of pointers

int * sol[2] ={&sol0,&sol1};
*sol[0]=1;

This works, but it's quite ugly in my opinion. Another solution is to use a tuple:

std::tuple<int&,int&> sol=std::tie(sol0,sol1);
std::get<0>(sol)=1;

Yet another option is to copy your values into an array, process the array, and copy them back.

Comments

0

Wouldn't a simple int array, solve your problem?!?

int sol[2];
for(int i=0; i<2; i++){
  sol[i]=(i+1)*(i+1);
  return max(sol[0],sol[1]);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.