1

My question is related to Boost.MultiArray class. I understand how to convert a multi_array object into a array_view object as the following codes show:

        int my_row = 5;
        int my_col = 7;
        boost::multi_array<int,2> my_matrix(boost::extents[my_row][my_col]);
        int *b = new int [my_row*my_col];
        int loop = 0;
        for(int i=0; i<my_row; i++)
        {
            for(int j=0; j<my_col; j++)
            {
                loop = loop+1;
                my_matrix[i][j]=loop;
            }
        }

        std::cout<<"The  matrix"<<std::endl;
        for (int i=0; i<my_row; i++)
        {
            for(int j=0; j<my_col; j++)
                std::cout<<my_matrix[i][j]<<"       ";
            std::cout<<std::endl;
        }

        int new_row = 3;
        int new_col = 4;
        boost::multi_array<int,2>::array_view<2>::type my_view = 
            my_matrix[boost::indices
            [boost::multi_array<int,2>::index_range(0,new_row,1)]
            [boost::multi_array<int,2>::index_range(0,new_col,1)]
            ];

        std::cout<<"The view"<<std::endl;
        for (int i=0; i<new_row; i++)
        {
            for(int j=0; j<new_col; j++)
                std::cout<<my_view[i][j]<<"     ";
            std::cout<<std::endl;
        }

My question is since the array_view object is very similar to multi_array object is there a way to transform the array_view object into a multi_array object?

1 Answer 1

2

boost::multi_array has these constructors that take an array_view:

multi_array(const const_array_view<NumDims>::type& x);
multi_array(const array_view<NumDims>::type& x);

You can construct a multi_array from your array_view with code like this:

boost::multi_array<int,2> array_from_view(my_view);

Note that the new multi_array has separate storage from the view and original array.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.