Boost logo

Boost Users :

From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-04-04 19:00:05


AMDG

Vladan Vidakovic wrote:
> Hi, I'm learning about multi_arrays and have not been able to find much
> info about different ways of initializing them. In particular I'm
> interested in initializing a multi_array in the same way I can init a
> basic C/C++ array:
>
> int array_2d[3][3] = { {1,2,3}, {2,3,4}, {3,4,5} };
>

That is impossible because multi_array is not an aggregate.

> When I declare an analogous multi_array, the compiler (VC++ 8.0)
> complains about the third line:
>
> typedef boost::multi_array< int, 2 > int_2d_array_t;
> boost::array< int_2d_array_t::index, 2 > shape = {{ 3, 3 }};
>
> int_2d_array_t A(shape) = { {1,2,3}, {2,3,4}, {3,4,5} }; // error
> C2143: syntax error : missing ';' before '='
>
>
> The online documentation only mentions initialization of individual
> elements (A[0][0][0] = 4;), but in doing it that way would be very
> tedious.
>
> What alternatives are there?
>

Nothing really wonderful, I hate to say. I wish that Boost.Assign worked.
The best I can come up with is:

#include <boost/multi_array.hpp>

using boost::assign::list_of;

int main() {
    boost::multi_array<int, 2> test(boost::extents[1][9]);
    int init[9] = { 1, 2, 3,
                    4, 5, 6,
                    7, 8, 9};
    std::copy(&init[0], &init[0] + 9, test[0].begin());
    boost::array<int, 2> shape = {3, 3};
    test.reshape(shape);
    //test.reshape(boost::extents[3][3]); // doesn't compile
}

In Christ,
Steven Watanabe


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net