如何使用(Boost多维数组库)构造动态二维数组?

发布于 2024-11-04 06:05:55 字数 985 浏览 4 评论 0 原文

我需要使用 boost 多维数组的帮助。我必须构造一个二维数组,其中: (0 <= j <= 1) 和 (i) 根据以下条件动态增长:

long boostArray[i][j];

因此,这就像构造一个由(未知)列和两行组成的表。

我已经开始使用 Boost Library 网站上提供的示例:

#include "boost/multi_array.hpp"
#include <cassert>

int main () {
  // 3 x 4 x 2 
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

问题是我没有彻底理解上面的代码,以便调整其结构并构建我想要的数组。我不知道如何在使用 Boost 库时准确地向数组添加元素/从数组中删除元素,特别是如果该数组如上所述动态增长的话。

例如,在处理向量时,我倾向于在调整向量大小后使用:push_backpop_back

I need help in using the boost multidimensional array. I have to construct a two dimensional array where: (0 <= j <= 1) and (i) grows dynamically according to:

long boostArray[i][j];

Thus, It's like constructing a table of (unknown) columns and two rows.

I started already with the example provided at the Boost Library website:

#include "boost/multi_array.hpp"
#include <cassert>

int main () {
  // 3 x 4 x 2 
  typedef boost::multi_array<double, 3> array_type;
  typedef array_type::index index;
  array_type A(boost::extents[3][4][2]);

  int values = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        A[i][j][k] = values++;

  int verify = 0;
  for(index i = 0; i != 3; ++i) 
    for(index j = 0; j != 4; ++j)
      for(index k = 0; k != 2; ++k)
        assert(A[i][j][k] == verify++);

  return 0;
}

The problem is that i didn't thoroughly understand the above code in order to tweak on its structure and build up my desired array. I don't know precisely how to add/delete elements to/from my array while using the Boost Library especially if this array grows dynamically as i described above.

For example, when dealing with vectors, i tend to use: push_back and pop_back after resizing the vector.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忆梦 2024-11-11 06:05:55

对于您的特定用例,您可能最好使用 vector>vector>。然后您可以使用 push_back,它非常高效。 boost::multi_array 听起来有点矫枉过正,otoh:

你不能在那里使用 push_back 之类的东西,因为每当你扩展 N 的一维时维数组,您需要提供初始数据的 N-1 维切片。这通常不是很有效,尤其是。因为你只能以这种方式添加到步幅最大的维度。您需要使用的是 resize 和赋值。

// std::vector<> equivalent (with vector<>, it's considered bad style)
v.resize( v.size() + 1 );
v[v.size()-1] = newElement;

// boost::multi_array (from the tutorial)
typedef boost::multi_array<int, 3> array_type;

array_type::extent_gen extents;
array_type A(extents[3][3][3]);
A[0][0][0] = 4;
A[2][2][2] = 5;
// here, it's the only way:
A.resize(extents[2][3][4]);
assert(A[0][0][0] == 4);
// A[2][2][2] is no longer valid.

重申一下:N 维数组 (N>2) 本质上比一维数组的动态性低得多(因为步幅因子)。上面的调整大小需要大量的数据复制,与向量情况不同,矢量情况只需要在 size()>capacity() 时复制数据。

For your particular usecase, you're probably better off using vector<pair<T,T>> or vector<array<T,2>>. You can then use push_back, and it's efficient. boost::multi_array sounds like overkill, otoh:

You can't use something like push_back there, because whenever you extend one dimension of an N-dimensional array, you'd need to supply a slice of N-1 dimensions of initial data. That is usually not very efficient, esp. since you can only add to the dimension with the largest stride in this way. What you need to use instead is resize and assignment.

// std::vector<> equivalent (with vector<>, it's considered bad style)
v.resize( v.size() + 1 );
v[v.size()-1] = newElement;

// boost::multi_array (from the tutorial)
typedef boost::multi_array<int, 3> array_type;

array_type::extent_gen extents;
array_type A(extents[3][3][3]);
A[0][0][0] = 4;
A[2][2][2] = 5;
// here, it's the only way:
A.resize(extents[2][3][4]);
assert(A[0][0][0] == 4);
// A[2][2][2] is no longer valid.

To reiterate: N-dimensional arrays, N>2, are inherently much less dynamic than one-dimensional ones (because of the stride factor). The above resize requires a lot of copying of the data, unlike the vector case, which only needs to copy data when size()>capacity().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文