[Rcpp-devel] fun(Times) with STL

Silkworth,David J. SILKWODJ at airproducts.com
Sat Jun 4 19:52:15 CEST 2011


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)




More information about the Rcpp-devel mailing list