I want to write a function that slices a \'string\' into a vector, sequentially, at a given index. I have a fairly adequate R solution for it; however, I figure that writing
I would use substring. Something like this:
strslice <- function( x, n ){
starts <- seq( 1L, nchar(x), by = n )
substring( x, starts, starts + n-1L )
}
strslice( "abcdef", 2 )
# [1] "ab" "cd" "ef"
About your Rcpp code, maybe you can allocate the std::vector with the right size, so that you avoid resizing it which might mean memory allocations, ... or perhaps directly use a Rcpp::CharacterVector. Something like this:
strslice_rcpp <- rcpp( signature(x="character", n="integer"), '
std::string myString = as(x);
int cutpoint = as(n);
int len = myString.length();
int nout = len / cutpoint ;
CharacterVector out( nout ) ;
for( int i=0; i