Dimension-independent loop over boost::multi_array?

前端 未结 4 1097
孤独总比滥情好
孤独总比滥情好 2020-12-19 06:00

Say I\'ve got an N-dimensional boost::multi_array (of type int for simplicity), where N is known at compile time but can vary (i.e. is a non-type template param

4条回答
  •  春和景丽
    2020-12-19 06:46

    There is a lack of simple boost multi array examples. So here is a very simple example of how to fill a boost multi array using indexes and how to read all the entries using a single pointer.

    typedef boost::multi_array array_type;
    typedef array_type::index index;
    
    array_type A(boost::extents[3][2]);
    
    //  ------> x 
    // | 0 2 4
    // | 1 3 5
    // v
    // y
    double value = 0;
    for(index x = 0; x < 3; ++x) 
        for(index y = 0; y < 2; ++y)
            A[x][y] = value++;
    
    double* it  = A.origin();
    double* end = A.origin() + A.num_elements();
    for(; it != end; ++it){
        std::cout << *it << " ";
    }
    
    // -> 0 1 2 3 4 5
    

提交回复
热议问题