[Rcpp-devel] fun(Times) with STL
A thinko on my part. Remove the '*' in front of the std::min_element. On Sat, Jun 4, 2011 at 12:52 PM, Silkworth,David J.
<SILKWODJ at airproducts.com> wrote:
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)
-------------- next part -------------- A non-text attachment was scrubbed... Name: foo.Rout Type: application/octet-stream Size: 1230 bytes Desc: not available URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20110604/daa93807/attachment.obj>