Skip to content
Prev 4913 / 10988 Next

[Rcpp-devel] RcppArmadillo: resize vs. insert_cols

Dear All,

I am using RcppArmadillo, and I am creating matrices in the C++ code
(i.e., these are not matrices passed from R). The sizes of these matrices
might need to increase dynamically during run time, with elements being
added at the end. What is the recommended way of doing this:


a) Just insert elements as needed. E.g.: Y.insert_cols(Y.n_cols, the_new_thing);

b) Create initial object. Double size of object (using resize) each time I
run out of space. Copy the_new_thing to the appropriate place.



I thought b) was a reasonable manual approach (e.g., Skiena, "The
algorithm design manual, 2nd ed", p. 67), but I think I've read that for
std::vector, a) (with push_back), is a better choice than b) (using
reserve, not resize).


For me, a) involves writing less code.  

Thanks,


R.