简体   繁体   English

迭代boost :: multi_array的维度

[英]Iterating over the dimensions of a boost::multi_array

I'm trying to write some dimension-independent code for a template class in c++, using a boost::multi_array (though if other containers/data structures are better at this, I'd be happy to hear about it). 我正在尝试使用boost :: multi_array为c ++中的模板类编写一些与维度无关的代码(尽管如果其他容器/数据结构更好,我会很高兴听到它)。

Given a dimension, I would like to iterate over the full range of every other dimension, returning a 1d view along the selected dimension. 给定一个维度,我想迭代所有其他维度的整个范围,沿所选维度返回1d视图。 This is fairly straightforward, or at least it appears to be from the boost documentation. 这是相当简单的,或者至少它似乎来自boost文档。

What I can't figure out how to do is iterate the selected dimension over all of the dimensions of the array, when the dimension of the array is known at compile time. 当编译时已知数组的维度时,我无法弄清楚如何做的是在数组的所有维度上迭代选定的维度。

Any advice on how to do this? 关于如何做到这一点的任何建议?

Normally you can do it with boost::multi_array, heres some sample code to create a 2D view of a 3D multi_array: 通常你可以用boost :: multi_array来做,下面是一些示例代码来创建3D multi_array的2D视图:

#include "boost/multi_array.hpp"
#include <cassert>
#include <iostream>

int main()
{
    typedef boost::multi_array<double, 3> array_type;
    typedef array_type::index index;

    array_type myArray3D(boost::extents[3][4][2]);

    // Assign values to the elements
    int values = 0;
    for (index i = 0; i != 3; ++i)
    {
            for (index j = 0; j != 4; ++j)
            {
                    for (index k = 0; k != 2; ++k)
                    {
                            myArray3D[i][j][k] = values++;
                    }
            }
    }
    // Verify values
    int verify = 0;
    for (index i = 0; i != 3; ++i)
    {
            for (index j = 0; j != 4; ++j)
            {
                    for (index k = 0; k != 2; ++k)
                    {
                            std::cout << "[" << i << "]";
                            std::cout << "[" << j << "]";
                            std::cout << "[" << k << "] = ";
                            std::cout << myArray3D[i][j][k] << std::endl;
                            assert(myArray3D[i][j][k] == verify++);
                    }
            }
    }

    typedef boost::multi_array_types::index_range range;
    array_type::index_gen indices;

    // Create a new view with 2 dimentions fixing the 2nd dimention to 1
    array_type::array_view<2>::type myView =
    myArray3D[indices[range()][1][range()]];
    std::size_t numDims = myView.size();
    std::cout << "numDims = " << numDims << std::endl;

    for (index i = 0; i != 3; ++i)
    {
            for (index j = 0; j != 2; ++j)
            {
                    std::cout << "[" << i << "]";
                    std::cout << "[" << j << "] = ";
                    std::cout << myView[i][j] << std::endl;
            }
    }

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM