【问题标题】:C++:How to have an array of boost::multi_arrayC++:如何拥有一个 boost::multi_array 数组
【发布时间】:2012-11-01 05:53:44
【问题描述】:

您好,我有几个 boost::multi_array 定义如下:

typedef boost::multi_array<double, 3> region_prior_integral_image

我正在尝试创建一个 region_prior_integral_image 数组,如下所示:

unordered_map<string, int> filename_to_hash_key_map = get_filename_to_hash_key_map();

unordered_map<string, region_prior_integral_image> filename_to_region_prior_map = get_region_prior_integral_images();

region_prior_integral_image* image_cache = new region_prior_integral_image[5];
for(unordered_map<string, int>::iterator it = filename_to_hash_key_map.begin(); it != filename_to_hash_key_map.end(); it++){
    image_cache[it->second] = filename_to_region_prior_map[it->first];
}

但是程序以以下方式终止:SemanticTextonForest: /home/aly/libs/boost_1_51_0/stage/include/boost/multi_array/multi_array_ref.hpp:488: boost::multi_array_ref&lt;T, NumDims&gt;&amp; boost::multi_array_ref&lt;T, NumDims&gt;::operator=(const ConstMultiArray&amp;) [with ConstMultiArray = boost::multi_array&lt;double, 3ul&gt;, T = double, long unsigned int NumDims = 3ul, boost::multi_array_ref&lt;T, NumDims&gt; = boost::multi_array_ref&lt;double, 3ul&gt;]: Assertionstd::equal(other.shape(),other.shape()+this->num_dimensions(), this->shape())' failed.`

我不知道为什么?

我知道我可以只使用一个向量,但为了论证,假设我想要一个 region_prior_integral_images 数组

谢谢

【问题讨论】:

    标签: c++ boost multidimensional-array boost-multi-array


    【解决方案1】:

    假设我们有两个region_prior_integral_image 实例:A 和B。如果要将B 分配给A,例如A = B;AB 的形状必须相等。错误消息是说,在您的代码image_cache[it-&gt;second] = filename_to_region_prior_map[it-&gt;first]; 中,这两个数组的形状不同。

    你是如何在filename_to_region_prior_map 中创建数组的?我猜你使用这个构造函数来指定形状:multi_array&lt;double,3&gt; B(boost::extents[i][j][k])。因此它们的形状是[i][j][k]。但是当您创建image_cache 时,会调用默认构造函数。所以这两个形状不匹配。

    我的意见是在你的代码中存储region_prior_integral_image的指针,这样也可以节省大量的拷贝。

    【讨论】:

    • 知道如何获取已创建的 region_prior_image 的尺寸吗?
    • 使用const size_type* shape() const; 喜欢:auto s = A.shape();,然后您可以通过s[0]s[1]s[2] 访问它。最好通读官方参考:boost.org/doc/libs/1_52_0/libs/multi_array/doc/reference.html
    • 如何创建给定大小的 region_prior_images 数组?
    • 不可能。使用原始数组时,只会调用默认构造函数。我建议改用std::vector,然后你可以用std::vector::resize(n, region_prior_image(boost::extents[i][j][k]))来填充它。
    猜你喜欢
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-12
    • 1970-01-01
    相关资源
    最近更新 更多