(This article was first published on

**Rcpp Gallery**, and kindly contributed to R-bloggers)The STL contains a very general looping or sweeping construct in the `for_each`

algorith. It can be used with function objects (such as the simple `square`

function used here) but also with custom class which can be used to keep to keep state.

```
#include <Rcpp.h>
using namespace Rcpp;
// somewhat silly little class derived from unary_function<T, void> to
// illustrate keeping state -- we interpret the vector x as containing
// growth rates (or returns), and we compute cumulative as well as
// relative gains.
template<class T> class cumProd : public std::unary_function<T, void> {
public:
cumProd() : cp(1.0), cnt(1) {} // constructor
void operator() (T x) { // default operator()
cp *= 1.0 + x;
Rcout << "Iteration " << cnt++
<< " Growth " << x
<< " Compounded " << cp
<< " Proportion " << x/(cp - 1.0)
<< std::endl;
}
private:
double cp;
int cnt;
};
// [[Rcpp::export]]
void forEach(Rcpp::NumericVector x) {
std::for_each(x.begin(), x.end(), cumProd<double>());
}
```

We can illustrate this on a simple example:

```
set.seed(42)
x <- rnorm(6, 0, 0.01)
x
```

[1] 0.013710 -0.005647 0.003631 0.006329 0.004043 -0.001061

```
forEach(x)
```

Iteration 1 Growth 0.0137096 Compounded 1.01371 Proportion 1 Iteration 2 Growth -0.00564698 Compounded 1.00799 Proportion -0.707182 Iteration 3 Growth 0.00363128 Compounded 1.01165 Proportion 0.31182 Iteration 4 Growth 0.00632863 Compounded 1.01805 Proportion 0.350659 Iteration 5 Growth 0.00404268 Compounded 1.02216 Proportion 0.182403 Iteration 6 Growth -0.00106125 Compounded 1.02108 Proportion -0.0503469

To

**leave a comment**for the author, please follow the link and comment on their blog:**Rcpp Gallery**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...