I have a 2d rectangular grid and I need to apply different functions to a subset of nodes in this grid. The subset is given by rectangular bounds e.g. row and column delimiters. Since I do not want to code the iteration with 2 for loops over and over again I am considering two approaches to solve this problem:
First create a custom iterator provider which is initialized using the rectangle limits and then keeps them while he is iterated. While this is feasible it seems quite some work to make this iterator compliant e.g. with standard stl aglorithms.
The second approach is to pass a function pointer into the function the traverses both for loops and execute it in the inner loop. This is feasible as well but might create quite ugly syntax since i have to pass member functions.
Which way is usually preferable ? And are there any clean examples for such a use case, to keep me from reinventing the wheel ?
Note: The operation is quite performance critical since the code is frequently executed
From design point of view I would say iterator approach is better, because it places less complexity on loop body (and likely there will be more complexity than in the iteration).
But I would expect better performance with functor approach. Especially if you will make it in STL-style (template parameter with expected operator() ).