How to use (Boost Multimimensional Array Library) to build a dynamic two-dimensional array?

I need help using a multidimensional boost array. I have to build a two-dimensional array, where: (0 <= j <= 1) and (i) is growing dynamically according to:

long boostArray[i][j];

So he would like to build a table of (unknown) columns and two rows.

I started with an example provided on 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 did not fully understand the above code in order to configure its structure and create my desired array. I do not know exactly how to add / remove elements to / from my array when using the Boost library, especially if this array is growing dynamically, as described above.

, : push_back pop_back .

+5
1

, , vector<pair<T,T>> vector<array<T,2>>. push_back, . boost::multi_array overkill, otoh:

- push_back , , N - , N-1 . , . . resize .

// std::vector<> equivalent (with vector<>, it 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 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().

+2

All Articles