Question

I want to learn how to add an one dimensional multi_array to end of another one dimensional multi_array. How would i do that?

Was it helpful?

Solution

Boost multi-array has (not very well documented) iterators like any other container, so you can use them as normal.

#include <boost/multi_array.hpp>
#include <iostream>

int
main  (int ac, char **av)
{
  typedef boost::multi_array<int, 1> array_type;
  array_type::extent_gen extents;
  //create some arrays
  array_type A(extents[3]);
  array_type B(extents[2]);
  //assign values
  A[0] = 4;
  A[1] = 3;
  A[2] = 5;
  B[0] = 1; 
  B[1] = 2;
  //resize A, (copies original values)  
  A.resize(extents[A.size()+B.size()]);
  //use iterators for copying
  std::copy(B.begin(), B.end(), A.end()-B.size());
  //check the output.
  for(size_t i=0;i<A.size();++i){
    std::cout<<A[i]<<std::endl;
  }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top