How to declare variables in a loop in C++?

Before me is the task simple task: to create several similar other variables with different names:
Counter1
Counter2
...
CounterN

How I imagine it:
in a for loop to declare different variables, indicating in brackets the value of i:

for (int i(1); i <= n; i++)
{
 int Counter{i} = i;
}


and the output to respectively Counter1; Counter2; Counter3.
It is obvious that my approach to solving this problem is not working.
Then the question is: how to solve me problem? Is it even possible? What are the alternative solutions in the absence of an adequate solution?
March 19th 20 at 08:30
2 answers
March 19th 20 at 08:32
Solution
In C++ you cannot create variables at runtime. You can do something like this:

std::vector<int> Counter;

for (int i = 0; i <= n; i++)
{
Counter.push_back(i);
}


Or use std::map for arbitrary keys:

std::map<int, int> Counter;

for (int i = 0; i <= n; i++)
{
 Counter[i] = i;
}
March 19th 20 at 08:34
for (int i = 0; i <= n; i++)
{
 int Counter[i] = i;
}
The solution is obvious, and I also tried to do so, however, I have climbs of the error and the code does not work:
1) the Expression must have a constant value (string int Counter[i] = i;)
2) the Expression is not defined by a constant (the same row)
3) Initializing: cannot convert "int" to "int [i]" (the same string) - tracey commented on March 19th 20 at 08:37

Find more questions by tags C++