Clean ways to write multiple 'for' loops

前端 未结 16 964
傲寒
傲寒 2020-12-22 15:22

For an array with multiple dimensions, we usually need to write a for loop for each of its dimensions. For example:

vector< vector< vector         


        
16条回答
  •  佛祖请我去吃肉
    2020-12-22 16:03

    One idea is to write an iterable pseudo-container class that "contains" the set of all multi-index tuples you'll index over. No implementation here because it'll take too long but the idea is that you should be able to write...

    multi_index mi (10, 8, 5);
      //  The pseudo-container whose iterators give {0,0,0}, {0,0,1}, ...
    
    for (auto i : mi)
    {
      //  In here, use i[0], i[1] and i[2] to access the three index values.
    }
    

提交回复
热议问题