[Boost-users] c++: boost::multi_array - initialize a multi_array

479 views
Skip to first unread message

Hannan Sadar

unread,
Dec 29, 2009, 9:52:40 AM12/29/09
to boost...@lists.boost.org
Hi all,

i am trying to initialize all the elements in boost::multi_array type to zero, so far, with out success. At tried using std::fill for 1-dim boost::multi_array and it worked. Once i tried std:fill on 2-dim boost::multi_array it haven't worked any more.

I saw boost have

#include
<boost/assign/std/vector.hpp> // for 'operator+=()'
#include
<boost/assert.hpp>;

libraries
but i don't know if this is the correct way to initialize a 2-dim multi_array.

I would like to know how to
initialize a boost::multi_array  in an elegant way.

here is an example code of my multi_array (it pass compilation, but return an error at run time):

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

namespace vec {
  typedef boost::multi_array<unsigned int, 1>  uint_1d_vec_t;
  typedef boost::multi_array<unsigned int, 2>  uint_2d_vec_t;
  typedef uint_1d_vec_t::index                 index_1d_t;
  typedef uint_2d_vec_t::index                 index_2d_t;
}

int main( ) {
   
    unsigned int num_elements, num_bits, max_runs, m;
    num_bits = 12;
    max_runs = 5000;
    m        = 2;

    num_elements = ( 1 << num_bits );
   
    int kappa = 79;

    vec::uint_1d_vec_t    foo( boost::extents[ static_cast< vec::index_1d_t >(num_elements) ]                                          );
    vec::uint_2d_vec_t    boo( boost::extents[ static_cast< vec::index_2d_t >(num_elements) ][ static_cast< vec::index_2d_t >(kappa) ] );

    std::fill( foo.begin()          , foo.end()        , 0);
    std::fill( boo.begin()->begin() , boo.end()->end() , 0);

    std::cout << "Done" << std::endl;

    return EXIT_SUCCESS;

}

any suggestion is welcome

regards

Rhys Ulerich

unread,
Dec 30, 2009, 12:54:57 PM12/30/09
to boost...@lists.boost.org
> i am trying to initialize all the elements in boost::multi_array type to
> zero, so far, with out success. At tried using std::fill for 1-dim
> boost::multi_array and it worked. Once i tried std:fill on 2-dim
> boost::multi_array it haven't worked any more.

One approach is the use multi_array::data() and
multi_array::num_elements() to get pointer-based iterators to the
underlying raw storage:

boost::multi_array<unsigned int, 1> foo( boost::extents[10] );
std::fill( foo.data(), foo.data() + foo.num_elements(), 0 );

boost::multi_array<unsigned int, 2> boo( boost::extents[10][79] );
std::fill( boo.data(), boo.data() + boo.num_elements(), 0 );

Another approach would be to use multi_array's assign member function.

HTH,
Rhys
_______________________________________________
Boost-users mailing list
Boost...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Reply all
Reply to author
Forward
0 new messages