2009-12-28 202 views
3

我已经与初始化升压阵列/的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 ]); 

定义boost::(multi_)arraynum_elements/2 IST的整数,kappa是双的,但仅包含整数(例如79)。

当它们中的元素数量仅在运行时才知道时,如何初始化fooboo0

+1

@Octavian:谢谢,但在标题中添加标签与清理相反。 – 2012-01-31 17:19:59

+0

@LightnessRacesinOrbit这似乎是个人观点。 [我如何写一个好的标题?](http://meta.stackexchange.com/questions/10647/how-do-i-write-a-good-title)。 – 2012-01-31 19:00:43

+2

@OctavianDamiean:这个主题的主流观点是标题中的标签不好。我们已经有了一个一致的索引标签系统。 Mods在过去也支持我进行这样的编辑。当然,不能取悦每个人。 – 2012-01-31 19:47:14

回答

-1
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) ]); 
+0

对不起,但我不明白我在这个解决方案中定义的初始化值在哪里? – Eagle 2009-12-28 20:51:25

+0

尽管此代码可能有助于解决问题,但 提供了有关_why_和/或_how_的其他上下文,回答该问题将显着提高其长期价值。请[编辑]你的答案,添加一些 的解释。 – 2016-07-15 12:08:20

-3

我用

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

解决我的问题(不知道这是否是更好然后升压::分配,因为我无法应用它)。

boo我仍然有问题,因为 std :: fill(boo.begin() - > begin(),boo.end() - > end(),0); 通编译,但是当我运行我的程序,我得到以下错误:

/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

这里是一个短代码:

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

请编辑您的原始问题,而不是添加不回答问题的答案。 – 2010-01-12 23:01:42

2

变化的线路

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

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

解决了我的问题

+1

您可能想使用'boo.data()'代替。 – updogliu 2012-12-29 08:13:56

+1

也可能想要使用'boo.num_elements()'而不是'boo.size()'。 – wedesoft 2014-09-05 09:52:48

+1

来自'.size()'的文档_返回a中包含的值的数量。它相当于a.shape()[0]; _同意@wedesoft,你应该使用'.num_elements()' – oLas 2015-12-18 16:54:13

-1

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