Skip to content

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

2 messages · Peng Yu, Douglas Bates

#
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;
for (int i=0; i<vec.size(); i++) {
v.push_back(vec[i]);
}
#
On Wed, Aug 22, 2012 at 9:55 AM, Peng Yu <pengyu.ut at gmail.com> wrote:
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.