Putting multiple calculations into a for-loop that uses variables based on iteration number

Since for each velocity, there will be an associated (and possibly distinct) currentPose and targetPoint, one way to do it it to have all these variables as std::vectors, or std::array if you know at compile time how many items you will have to store. Then your loop could look like this:

for (int i=0; i<X; i++)
{
  velocity[i] = computeVelocity(currentPose[i], targetPoint[i]);
}

I don’t think that wanting the i to be a part of the variables’ name is doable (although there might be some way to do it using preprocessor macros and the # concatenation operator, I have not thought about it), nor would it be usual C++ code.

For a C++ programmer the vector/array approach is the more natural one.

Leave a Comment