Originally Posted by
fclopez
I hope it makes the aim clearer
It is just a restatement of the original question. There is no way in C++ to concatenate an integral value computed in a loop to a variable name, in order to create another variable name. Restating the problem does not make it solvable if it is not already solvable. C++ is not PHP, and cannot be used as if it is.
What you need to do is create an array (or a container) of vectors. There are various options.
Code:
std::vector<std::vector<float> > someVector(20); // creates a vector containing 20 vectors.
for(int ivector = 0; ivector<20; ivector++)
{
for(int i = 0; i<1000; i++)
{
y = x*i*ivector;
someVector[ivector].push_back(y); // this is a functional equivalent of what you have described as someVector_%d.push_back(y)
// or, alternatively, someVector.at(ivector).push_back(y);
}
}
or (using a naked array, like described by iMalc)
Code:
std::vector<float> someVector[20]; // creates a array containing 20 vectors.
for(int ivector = 0; ivector<20; ivector++)
{
for(int i = 0; i<1000; i++)
{
y = x*i*ivector;
someVector[ivector].push_back(y); // this is a functional equivalent of what you have described as someVector_%d.push_back(y)
}
}
or using the std::array approach advocated as the bees knees by Elysia (although it relies on you having access to C++-11 compliant compiler)
Code:
std::array<std::vector<float>, 20> someVector; // creates a array containing 20 vectors.
for(int ivector = 0; ivector<20; ivector++)
{
for(int i = 0; i<1000; i++)
{
y = x*i*ivector;
someVector[ivector].push_back(y); // this is a functional equivalent of what you have described as someVector_%d.push_back(y)
// or, alternatively, someVector.at(ivector).push_back(y);
}
}
There are differences between these, and each technique has advantages and disadvantages.
The first and second are supported by all versions of C++ (i.e. by the standard since 1998). The third method is not.
The first approach allows the number of vectors to be changed later, the second and third approaches do not.
The first and third approaches are considered "safer" by some if the someVector.at(ivector) is used instead of someVector[ivector] (assuming you know how to catch and recover from exceptions, as that is how the at() approach reports errors it detects). In comparison, the second approach is considered more dangerous (in the sense of robustness to programmer mistakes, such as making 20 vectors and accessing the 30th one). However, if used incorrectly, any of the approaches can yield invalid behaviour (writing over memory it shouldn't).