[英]Initialize boost array/multi_array
I've defined boost::(multi_)array
with 我用boost::(multi_)array
定义了boost::(multi_)array
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 and kappa
is a double, but contains only integer numbers (for example 79). num_elements/2
是整数, kappa
是double,但只包含整数(例如79)。
How do can I initialize foo
and boo
to 0
, when the number of elements in side them is known only at runtime? 如果仅在运行时知道它们中的元素数量,我怎么能将foo
和boo
初始化为0
?
changing line 换线
std::fill( boo.begin()->begin() , boo.end()->end() , 0);
to 至
std::fill( boo.origin(), boo.origin() + boo.size(), 0 );
solved my problem 解决了我的问题
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 ) ] );
您可能还希望使用calloc,然后使用boost :: multi_array_ref包装返回的内存。
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). 解决我的问题(不知道它是否更好然后boost :: assign,因为我无法应用它)。
with boo
i still have problem, since std::fill( boo.begin()->begin() , boo.end()->end() , 0); 与boo
我还有问题,因为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 /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_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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.