[Rcpp-devel] fun(Times) with STL

Wray, Christopher christopher.wray.10 at ucl.ac.uk
Sat Jun 4 20:09:16 CEST 2011


try:
int min_el_ind = std::min_element(TimeQ.begin(), TimeQ.end()) - TimeQ.begin();

________________________________________
From: rcpp-devel-bounces at r-forge.wu-wien.ac.at [rcpp-devel-bounces at r-forge.wu-wien.ac.at] on behalf of Silkworth,David J. [SILKWODJ at airproducts.com]
Sent: 04 June 2011 18:52
To: rcpp-devel at r-forge.wu-wien.ac.at
Cc: dmbates at gmail.com
Subject: Re: [Rcpp-devel] fun(Times) with STL

Many thanks, Doug, I feel so-o-o close, but the revised example does not compile.
I have taken the liberty to condense our discussion a little here.

>>> double* myIterator = std::min_element (TimeQ.begin(), TimeQ.end());
>>
>> The usual idiom is
>>
>> double TQmin = *std::min_element(TimeQ.begin(), TimeQ.end());

>Sorry.  I didn't read through to the end of your message.  If you want
>the index of the minimum element you can use

>int min_el_ind = *std::min_element(TimeQ.begin(), TimeQ.end()) - TimeQ.begin();

>The beauty of iterators is that they have more flexibility than simple
>pointers and they also carry more information, so that the difference
>between two iterators is the index of an element in an array-like
>structure.

Here is the revised example as I have distilled it down and edited (does NOT compile):

src <- '
Rcpp::NumericVector TimeQ(arg1);
Rcpp::IntegerVector show_position(1);
int min_el_ind = *std::min_element(TimeQ.begin(), TimeQ.end()) - TimeQ.begin();
show_position[0] = min_el_ind;
return show_position;
'
 fun <- cxxfunction(signature(arg1="numeric"),src,plugin="Rcpp")
Times<-c(1944.285,2920.969,1720.230,1264.438,3607.507,1720.230,25176.020);
fun_test<- fun(Times)


_______________________________________________
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


More information about the Rcpp-devel mailing list