Reuse nested loops without copy and paste

2019-02-09 17:06发布

Suppose I've this nested loop

for (int a=1; a<MAX_A; ++a)
  for (int b=1; b<MAX_B; ++b)
    for (int c=1; c<MAX_C; ++c)
    {
       do_something(a, b ,c);
    }

and I reuse this loop in various part of my code, changing the function do_something. It's quite boring to rewrite every time the first three lines. In python for example I would created a generator to return an iterator (1, 1, 1), (1, 1, 2), ... or something like itertools.product.

In c++ the only solution I've in mind is to define a macro. Something better?e

2条回答
够拽才男人
2楼-- · 2019-02-09 17:12

Make a helper loop function:

template<typename Work>
void loop(Work work) {
  for (int a=1; a<MAX_A; ++a)
    for (int b=1; b<MAX_B; ++b)
      for (int c=1; c<MAX_C; ++c)
      {
        work(a, b ,c);
      }
}

Define the work to be done:

void some_work(int a, int b, int c) {
}

Then use:

loop(some_work);

Note that now you don't need to use only function, but you can use a function object (object for which an operator() is overloaded).

查看更多
Anthone
3楼-- · 2019-02-09 17:14

Use templates:

template<typename Func>
inline void do_something_loop(Func f)
{
    for (int a=1; a<MAX_A; ++a) 
      for (int b=1; b<MAX_B; ++b) 
        for (int c=1; c<MAX_C; ++c) 
        { 
           f(a, b ,c); 
        } 
}

This can be called with any function pointer or function object that matches the signature, e.g.:

void do_something(int a, int b, int c) { /* stuff */ }

do_something_loop(do_something);

Or with a function object:

struct do_something
{
    void operator()(int a, int b, int c) { /* stuff */ }
};

do_something_loop(do_something()); 

Or if your compiler supports C++11, even with a lambda:

do_something_loop([](int a, int b, int c) { /* stuff */ });

Note that you could also declare the f parameter as a function pointer with the signature void(*f)(int,int,int) instead of using a template, but that's less flexible (it won't work on function objects (including the result of std::bind) or lambdas).

查看更多
登录 后发表回答