[Rcpp-devel] Convertion from SEXP to std::vector<unsigned>

Douglas Bates bates at stat.wisc.edu
Wed Aug 22 18:53:37 CEST 2012

On Wed, Aug 22, 2012 at 9:55 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
> Hi,
> Rcpp::IntegerVector can not be converted to std::vector<unsigned>. Is
> there a class that can help convert SEXP to std::vector<unsigned>?
> The current walkaround can be something like the following. I'm
> wondering if there is a better solution?
> Rcpp::IntegerVector vec(vx);

> std::vector<unsigned> v;

You should at least specify the size there so that push_back doesn't
need to allocate/reallocate and possibly overallocate.

std::vector<unsigned> v(vec.size());

You may be able to allocate from iterators as in

std::vector<unsigned> v(vec.begin(), vec.end());

but I'm not sure if that will do the automatic conversion from signed
int to unsigned.  Look up the different forms of constructors for
std::vector and the STL algorithms such as std::transform.

> for (int i=0; i<vec.size(); i++) {
> v.push_back(vec[i]);
> }
> --
> Regards,
> Peng
> _______________________________________________
> Rcpp-devel mailing list
> Rcpp-devel at lists.r-forge.r-project.org
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel

More information about the Rcpp-devel mailing list