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

Dirk Eddelbuettel edd at debian.org
Sun Nov 13 18:03:41 CET 2011


On 13 November 2011 at 10:33, Dirk Eddelbuettel wrote:
| 
| On 13 November 2011 at 09:52, Steve Lianoglou wrote:
| | 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.
| 
| IIRC several of the intro docs should hsve it, same with the lidt srchive.
| 
| | 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_);
| 
| a)  Just create a new stl vector from x here, or
| 
| b)  Use x.begin() in the call to something:  something(x.begin());

Err, sorry. Lack of coffee.  That just gets you a double* for a C interface.
Just use the as<> wrapper.

Dirk

| 
| Hth, Dirk
| 
| 
| |   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
| | _______________________________________________
| | 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
| 
| -- 
| "Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
| dark to read." -- Groucho Marx
| _______________________________________________
| 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

-- 
"Outside of a dog, a book is a man's best friend. Inside of a dog, it is too
dark to read." -- Groucho Marx


More information about the Rcpp-devel mailing list