[Rcpp-devel] Passing NumericVector as std::vector<double>?

Steve Lianoglou mailinglist.honeypot at gmail.com
Sun Nov 13 15:52:43 CET 2011


Hi,

Sorry if I've missed the obvious examples/documentation .. this seems
like an easy enough scenario that must be documented somewhere, but
I'm not seeing it.

Imagine that the C++ code I am wrapping w/ Rcpp expects
std::vector<XXX> objects in its API. Is there an easy way for me to
convert an Rcpp NumericVector (or IntegerVector) to an std::vector so
the compiler doesn't complain?

Currently, in my .Call interface function, I might have something like:

SEXP Rsomething(SEXP x_) {
  std::vector<double> x = Rcpp::as< std::vector<double> >(x_);
  std::vector<double> result = something(x); // something defined in C++ lib
  return Rcpp::wrap(result);
}

But, can I do something like:

SEXP Rsomething(SEXP x_) {
  Rcpp::NumericVector x(x_);
  std::vector<double> result = something(x.TO_STL()); // or something
  return Rcpp::wrap(result);
}

Thanks,
-steve

-- 
Steve Lianoglou
Graduate Student: Computational Systems Biology
 | Memorial Sloan-Kettering Cancer Center
 | Weill Medical College of Cornell University
Contact Info: http://cbio.mskcc.org/~lianos/contact


More information about the Rcpp-devel mailing list