我需要使用 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_back和pop_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.
发布评论
评论(1)
对于您的特定用例,您可能最好使用
vector>
或vector>
。然后您可以使用push_back
,它非常高效。boost::multi_array
听起来有点矫枉过正,otoh:你不能在那里使用
push_back
之类的东西,因为每当你扩展N
的一维时维数组,您需要提供初始数据的N-1
维切片。这通常不是很有效,尤其是。因为你只能以这种方式添加到步幅最大的维度。您需要使用的是resize
和赋值。重申一下:
N
维数组 (N>2
) 本质上比一维数组的动态性低得多(因为步幅因子)。上面的调整大小需要大量的数据复制,与向量情况不同,矢量情况只需要在size()>capacity()
时复制数据。For your particular usecase, you're probably better off using
vector<pair<T,T>>
orvector<array<T,2>>
. You can then usepush_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 anN
-dimensional array, you'd need to supply a slice ofN-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 isresize
and assignment.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 whensize()>capacity()
.