1

我是 Boost 的新手(也是 stackoverflow 的新手)并且想要使用向量的多数组。我是这样做的:

typedef boost::multi_array<std::vector<Vector3_t>, 2> array_type;
array_type* mImage;
int mResolution = 1000;
mImage = new array_type (boost::extents[mResolution][mResolution]);
//works  
mImage[0][0].origin()->push_back(Vector3_t());
//Error: abort()
mImage[1][1].origin()->push_back(Vector3_t());
//Error: abort()
mImage[500][440].origin()->push_back(Vector3_t());

在互联网上,我只能找到使用 int、doule 等的多数组示例。是否可以在 mutliarray 中使用 std::vector ?我知道我可以使用 3d 多数组,但我更喜欢向量作为元素。

4

1 回答 1

2

Boost.MultiArray 支持std::vector元素。通常,Boost.MultiArray 将在编译时执行概念检查。因此,如果代码使用完整类型编译,那么它应该被支持。

mImage[0][0].origin()

  • mImage[0][0]返回对 的引用std::vector<Vector3_t>
  • origin()不是 上的成员函数std::vector<Vector3_t>,导致错误。

origin()是多数组的成员函数,它返回第一个元素的存储地址。在数组没有被重新索引为正索引的情况下,这相当于0所有索引(即mImage.origin() == &mImage[0][0])。


这是一个简短而完整的示例,其中包含整数向量的多数组。

#include <iostream>
#include <vector>

#include <boost/foreach.hpp>
#include <boost/range/counting_range.hpp>
#include <boost/multi_array.hpp>

int main()
{
  typedef std::vector<int> vector3_type;
  typedef boost::multi_array<std::vector<vector3_type>, 2> array_type;
  array_type array(boost::extents[5][5]);

  // Insert vector into multi-array.
  array[0][0].push_back(vector3_type());

  // Insert range of [100,105) into the first vector at [0][0] 
  BOOST_FOREACH(const int& i, boost::counting_range(100, 105))
    array[0][0].front().push_back(i);

  // Print all integers at [0][0][0]
  BOOST_FOREACH(const int& i, array[0][0][0])
    std::cout << i << std::endl;
}

运行会产生以下预期输出:

100
101
102
103
104
于 2013-05-14T16:40:44.657 回答