C++ 初始化增压阵列/多功能阵列

C++ 初始化增压阵列/多功能阵列,c++,arrays,boost,initialization,C++,Arrays,Boost,Initialization,我已经用 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_ele

我已经用

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 ] );
typedef boost::多数组uint\u 1d\u vec\t;
typedef boost::多数组uint\u 2d\u vec\t;
uint_1d_vec_t foo(boost::extensts[num_元素]);
uint_2d_vec_t boo(boost::extenses[num_elements/2][kappa]);
WARE
num\u elements/2
是一个整数,
kappa
是一个双精度数,但只包含整数(例如79)

只有在运行时才知道其中的元素数,如何将
foo
boo
初始化为
0
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 ) ] );
uint二维向量boo(boost::extenses[静态向量(num\u elements/2)][静态向量(kappa)]; 我用过

std::fill( foo.begin() , foo.end()    , 0);
解决我的问题(不知道它是否比boost::assign更好,因为我无法应用它)

对于
boo
我仍然有问题,因为 std::fill(boo.begin()->begin(),boo.end()->end(),0); 通过编译,但一旦运行程序,就会出现以下错误:

/usr/include/boost/multi_array/base.hpp:178:参考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]:断言'size\u type(idx-index\u base[0]) 下面是一个简短的代码:

#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;
}
#包括
#包括“boost/multi_array.hpp”
#包括
命名空间向量{
typedef boost::多阵列uint\u 1d\u vec\t;
typedef boost::多数组uint\u 2d\u vec\t;
typedef uint_1d_vec_t::index index index_1d_t;
typedef uint_2d_vec_t::index index_2d_t;
}
使用名称空间std;
int main(){
无符号整数num_元素,num_位,最大运行次数,m;
num_位=12;
最大运行次数=5000次;
m=2;
num_元素=(1(num_元素)];
vec::uint_2d_vec_t boo(boost::区段[静态投射(num_元素)][静态投射(kappa)];
std::fill(foo.begin(),foo.end(),0);
std::fill(boo.begin()->begin(),boo.end()->end(),0);
std::cout换行

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


解决了我的问题

您可能还希望使用calloc,然后用boost::multi_array_ref包装返回的内存。

对不起,但我不知道在这个解决方案中我应该在哪里定义初始化值?虽然这段代码可能有助于解决问题,但提供了有关为什么和/或如何回答问题的其他上下文显著提高其长期价值。请您的答案添加一些解释。请编辑您的原始问题,而不是添加不回答该问题的答案。@Octavian:谢谢,但在标题中添加标记与清除相反。@LightnessRacesinOrbit这似乎是个人观点的问题。@OctavianDamiean:在这个问题上,流行的观点是标题中的标签是不好的。我们已经有了一个一致的索引标签系统。Mods在过去也支持我进行此类编辑。当然,不能让每个人都满意。你可能想使用
boo.data()
。也可能想使用
boo.num\u elements()
而不是
boo.size()
。从
.size()
的文档中,这将返回a中包含的值的数量。它相当于a.shape()[0];同意@wedesoft,您应该使用
.num\u elements()
  std::fill( boo.origin(), boo.origin() + boo.size(), 0 );