title | author | license | tags | summary | layout | src |
---|---|---|---|---|---|---|
STL Transform |
Dirk Eddelbuettel |
GPL (>= 2) |
stl |
This example shows how to use the STL's transform function |
post |
2012-12-28-stl-transform.cpp |
The STL transform function can be used to pass a single function over
a vector. Here we use a simple function square()
.
{% highlight cpp %} #include <Rcpp.h>
using namespace Rcpp;
inline double square(double x) { return x*x ; }
// [[Rcpp::export]] std::vector transformEx(const std::vector& x) { std::vector y(x.size()); std::transform(x.begin(), x.end(), y.begin(), square); return y; } {% endhighlight %}
{% highlight r %} x <- c(1,2,3,4) transformEx(x) {% endhighlight %}
[1] 1 4 9 16
A second variant combines two input vectors.
{% highlight cpp %} inline double squaredNorm(double x, double y) { return sqrt(xx + yy); }
// [[Rcpp::export]] NumericVector transformEx2(NumericVector x, NumericVector y) { NumericVector z(x.size()); std::transform(x.begin(), x.end(), y.begin(), z.begin(), squaredNorm); return z; } {% endhighlight %}
{% highlight r %} x <- c(1,2,3,4) y <- c(2,2,3,3) transformEx2(x,y) {% endhighlight %}
[1] 2.236 2.828 4.243 5.000