2

I have a scenario in my c++ project which necessitates the use of a dynamically expanding array within a for loop and it is not conducive or highly inefficient to preallocate. Simply put, after each iteration of the loop the array is appended by a sub-array of length that is different from the previous iteration. One possible solution is std::vector::push_back. But, currently I rely heavily on the armadillo library. My questions are as follows,

  1. Is a push_back like operation possible with armadillo armadillo Col, Colvec or mat data structures?

  2. If (1) is not possible, Is it easy/efficient to switch back and forth between std::vector and armadillo vectors?

  3. Is the use of arma::fields an efficient alternative? The idea is to allocate a field of size equal to the range of the loop.
    Copy vectors into the field elements during each iteration of the for loop and once the loop is exited, consolidate the field elements into a single vector.

Naveen
  • 458
  • 1
  • 10
  • 29
  • 2
    http://arma.sourceforge.net/docs.html#resize_member – Tezirg Jun 01 '16 at 15:42
  • The link does not tell you how ot will compare to std::vectors at all. What is needed is a comparison how memory is handled. – dani Jun 04 '16 at 08:40
  • 1
    In similar conditions I went with (2): grow `std::vector` and convert back with `conv_to`. There's a copy, obviously, but it's critical, look into setting `copy_aux_mem = false` (advanced constructors subsection). – tonytonov Jun 06 '16 at 11:33
  • I think @Tenzirg's link answers the question. The resize() function in armadillo preserves the elements in the array while resizing, unlike valarray::resize() which formats the existing array. – Naveen Jun 06 '16 at 14:02

0 Answers0