[Rcpp-devel] [slightly OT] Can std::transform be used in place?

Dirk Eddelbuettel edd at debian.org
Thu May 6 04:38:11 CEST 2010


On 5 May 2010 at 14:45, Douglas Bates wrote:
| I haven't seen in any of the descriptions of the std::transform
| template whether it can modify values in place.  For example, can I
| perform the equivalent of the
| 
| x <- x * mult
| 
| operation in R, where x and mult are both numeric n-dimensional vectors, as
| 
| std::transform(x.begin(), x.end(), mult.begin(), x.begin(),
| std::multiplies<double>());
| 
| in Rcpp-based C++?

It should (as Davor already posted).  I'd test it, which gets you to add yet
another regression test :)

One thing you may find handy on your Ubuntu machine(s) is

       wajig install c++-annotations-pdf
       xdg-open /usr/share/doc/c++-annotations/cplusplusus.pdf

which is really nicely done 700+ tome on C++ by Frank Brokken (who is both
upstream author and Debian maintainer).  

Ubuntu 9.10 is at version 7.30; Ubuntu 10.4 is at 8.1.0 and Debian testing at
8.1.1.  I already liked the older versions, and 8.* improves further on the
already very good 7.* series.  Now, version 7.30 says in 'Section 17.4.63
transform()' (and hooray to kpdf for letting me copy text from the pdf):

17.4.63   transform()
  • Header file:
              #include <algorithm>

  • Function prototypes:
      – OutputIterator transform(InputIterator first, InputIterator last,
        OutputIterator result, UnaryOperator op);
      – OutputIterator transform(InputIterator1 first1, InputIterator1 last1,
        InputIterator2 first2, OutputIterator result, BinaryOperator op);

  • Description:
      – The first prototype: the unary operator op is applied to each of the elements in the range
        [first, last), and the resulting values are stored in the range starting at result.
        The return value points just beyond the last generated element.
      – The second prototype: the binary operator op is applied to each of the elements in the
        range [first1, last1) and the corresponding element in the second range starting at
        first2. The resulting values are stored in the range starting at result. The return
        value points just beyond the last generated element.

and an example follows.

For those without Debian / Ubuntu, these books are also at http://c++annotations.sf.net

-- 
  Regards, Dirk


More information about the Rcpp-devel mailing list