I have a vector of arbitrary size, and need to create a for
loop from each entry from zero up to that number.
For example, if my list was {2,3,4}
, I would need the following to run in my program.
for (unsigned int i = 0; i < 2; ++i}
for (unsigned int j = 0; j < 3; ++j}
for (unsigned int k = 0; k < 4; ++k}
{ do stuff }
My problem is that I don't know how many elements there are, and so I can't explicitly state the number of for
loops that I need. I've thought about making a class that contains the list and overloading the +
operator, iterating until the lists are equal, but I was hoping there was something that wasn't so messy. If not, I will have to be ok with that, I guess. Is there an cleaner way to do this than what I was thinking?
The reason that I need this is to iterate over all elements of an n-dimensional matrix, so my matrix could be 2 x 3 x 4
in the case I presented, but could have an arbitrary number of dimensions.
Thanks for any input.