可变参数嵌套循环 [英] Variadic nested loops

查看:149
本文介绍了可变参数嵌套循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的工作A N维网格。
我想生成依赖于任何尺寸(2D,3D,4D等)嵌套循环。
我怎样才能做到这在一个优雅的,快捷的方式?下面我的问题的一个简单的例子。 我写在C + +,但我认为这样的问题可以为其它语言是有用的。照片 我需要知道指数在我做的东西的一部分(I,J,K ...)。 编辑:LOWER_BOUND和UPPER_BOUND重新presents网格中的,所以他们总是积极的指标

 的#include<载体>

诠释的main()
{
    //尺寸这里是3D
    的std ::矢量<为size_t> LOWER_BOUND({4,2,1});
    的std ::矢量<为size_t> UPPER_BOUND({16,47,9});

    用于(为size_t I = LOWER_BOUND [0]; I< UPPER_BOUND [0];我++)
        为(为size_t J = LOWER_BOUND [1]; J&其中; UPPER_BOUND [1]; J ++)
            用于(为size_t K = LOWER_BOUND [2]; K< UPPER_BOUND [2]; k ++)
                //对于(为size_t升= LOWER_BOUND [3]; L&其中; UPPER_BOUND [3];升++)
                // ...
                {
                    //做的东西,如
                    格({I,J,K})= 2 * i + 3中*的J  -  4 * K表;
                    //其中网格大小是顶点的总数
                }
}
 

解决方案

下面可能会有帮助:

 布尔增量(
    的std ::矢量< INT>&安培; V,
    常量的std ::矢量< INT>&安培;降低,
    常量的std ::矢量< INT>&安培;上)
{
    断言(v.size()== lower.size());
    断言(v.size()== upper.size());

    为(自动I = v.size();我 -  = 0;!){
        ++ v [电流]
        如果(V [I]!=上[I]){
            返回true;
        }
        v [电流] =低[I]
    }
    返回false;
}
 

和使用这种方式:

  INT主要(){
    常量的std ::矢量< INT> LOWER_BOUND({4,2,1});
    常量的std ::矢量< INT> UPPER_BOUND({6,7,4});
    的std ::矢量< INT>电流= LOWER_BOUND;

    做 {
        性病::复制(current.begin(),current.end(),性病:: ostream_iterator< INT>(性病::法院));
        性病::法院<<的std :: ENDL;
    }而(增量(电流,LOWER_BOUND,UPPER_BOUND));
}
 

现场演示

I am working on a N dimensional grid.
I would like to generate nested loops depending on any dimension (2D, 3D, 4D, etc...).
How can I do that in an elegant and fast way ? Below a simple illustration of my problem. I am writing in C++ but I think this kind of question can be useful for other languages.
I need to know the indices (i,j,k...) in my do stuff part. Edit : lower_bound and upper_bound represents the indexes in the grid so they are always positive.

#include <vector>

int main()
{
    // Dimension here is 3D
    std::vector<size_t> lower_bound({4,2,1});
    std::vector<size_t> upper_bound({16,47,9});

    for (size_t i = lower_bound[0]; i < upper_bound[0]; i ++)
        for (size_t j = lower_bound[1]; j < upper_bound[1]; j ++)
            for (size_t k = lower_bound[2]; k < upper_bound[2]; k ++)
                // for (size_t l = lower_bound[3]; l < upper_bound[3]; l ++)
                //  ...
                {
                    // Do stuff such as
                    grid({i,j,k}) = 2 * i + 3 *j - 4 * k;
                    // where grid size is the total number of vertices
                }
}

解决方案

Following may help:

bool increment(
    std::vector<int>& v,
    const std::vector<int>& lower,
    const std::vector<int>& upper)
{
    assert(v.size() == lower.size());
    assert(v.size() == upper.size());

    for (auto i = v.size(); i-- != 0; ) {
        ++v[i];
        if (v[i] != upper[i]) {
            return true;
        }
        v[i] = lower[i];
    }
    return false;
}

And use it that way:

int main() {
    const std::vector<int> lower_bound({4,2,1});
    const std::vector<int> upper_bound({6,7,4});
    std::vector<int> current = lower_bound;

    do {
        std::copy(current.begin(), current.end(), std::ostream_iterator<int>(std::cout, " "));
        std::cout << std::endl;
    } while (increment(current, lower_bound, upper_bound));
}

Live demo

这篇关于可变参数嵌套循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆