views:

737

answers:

4

Hi,

i defined boost::multi_array with

typedef boost::multi_array<unsigned int, 1>  uint_1d_vec_t;
typedef boost::multi_array<unsigned int, 2>  uint_2d_vec_t;

uint_1d_vec_t    foo( boost::extents[ num_elements   ]          );
uint_2d_vec_t    boo( boost::extents[ num_elements/2 ][ kappa ] );

were num_elements/2 ist an integer number and kappa is a double, but contain only integer numbers ( e.g 79).

i would like to know how could i initialize foo and boo to 0, when i know only in runtime the number of elements in side them.

Thanks

+1  A: 
uint_1d_vec_t    foo( boost::extents[ static_cast< uint_1d_vec_t::index >( num_elements   ) ]          );
uint_2d_vec_t    boo( boost::extents[ static_cast< uint_2d_vec_t::index >( num_elements/2 ) ][ static_cast< uint_2d_vec_t::index >( kappa ) ] );
baton
sorry, but i dont understard where do i define in this solution the initialize value?
Eagle
A: 

i used

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

to solve my problem (don't know if it is better then boost::assign, since i was unable to apply it).

with boo i still have problem, since std::fill( boo.begin()->begin() , boo.end()->end() , 0); pass compilation, but once i run my program, i get the following error:

/usr/include/boost/multi_array/base.hpp:178: Reference boost::detail::multi_array::value_accessor_one::access(boost::type, boost::multi_array_types::index, TPtr, const boost::multi_array_types::size_type*, const boost::multi_array_types::index*, const boost::multi_array_types::index*) const [with Reference = unsigned int&, TPtr = unsigned int*, T = unsigned int]: Assertion `size_type(idx - index_bases[0]) < extents[0]' failed.Blockquote

here is a short code:

#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;
}

using namespace std;

int main( ) { 

   unsigned int num_elements, num_bits, max_runs, m;
   num_bits = 12;
   max_runs = 5000;
   m        = 2;

   num_elements = ( 1 << num_bits );

   double 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;
}
Eagle
Please edit your original question instead of adding answers that do not answer the question.
Alex B
A: 

changing line

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

to

  std::fill( boo.origin(), boo.origin() + boo.size(), 0 );

solved my problem

Eagle
A: 

You may also wish to use calloc and then wrap the returned memory with a boost::multi_array_ref.

Rhys Ulerich