<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=us-ascii">
<META NAME="Generator" CONTENT="MS Exchange Server version 6.5.7654.12">
<TITLE>fun(Times) with STL</TITLE>
</HEAD>
<BODY>
<!-- Converted from text/rtf format -->

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">I ran into this yesterday as I needed to get the position of the least time value in a small</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">Que of times that I had extracted from a more complex structure</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">.&nbsp;</FONT></SPAN><SPAN LANG="en-us">&nbsp;<FONT FACE="Calibri"> I ended up</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">with this problem during a separate</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri"> work-around</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">, which is</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">a story I</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">&#8217;</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">d love to tell also</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">.</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">But, to the point.&nbsp; I explored</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">using STL in areas I had seen no examples previously.</FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Nothing in the Rcpp sources suggested that min_element() or max_element</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">() functions were supported, but I tried anyway.</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Only by taking note of the many compiler errors I generated was I able to come up with this stable line</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri"> of code</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">:</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">double* myIterator = std::min_element (TimeQ.begin(), TimeQ.end());</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Now, in C++</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">training</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri"> I flunked pointers and I skipped class for templates.</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">&nbsp; But now I need to know what the heck is myIterator.</FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">So, I managed to put it into a single element Numeric</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">Vector which I could return to R and examine.</FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Okay, so this mysterious</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">&#8220;</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">iterator</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">&#8221;</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri"> thing is quite intuitively the expected result of a min_element function</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri"> (with some pointer witchcraft included).</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">To get the position of this item in the TimeQ I ended up building a small loop.</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">for(int col=0; col&lt;TimeQ.size(); col++)&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">if(TimeQ[col] == *myIterator) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">show_position[0]=col;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">break;&nbsp; }&nbsp;&nbsp; }</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">My question is,</FONT></SPAN><SPAN LANG="en-us"> <FONT FACE="Calibri">&#8220;</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">I</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">s there a more elegant way to get the position value that I need</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">&#8221;</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">?</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri"></FONT></SPAN><SPAN LANG="en-us"> &nbsp;&nbsp; <FONT FACE="Calibri">This code will be traversed 10</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">&#8217;</FONT></SPAN><SPAN LANG="en-us"><FONT FACE="Calibri">s of thousands of times in the function I am developing.</FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Here is the full example as I have distilled it down:</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">src &lt;- '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Rcpp::NumericVector TimeQ(arg1);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Rcpp::NumericVector show_iterator(1);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Rcpp::IntegerVector show_position(1);&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">double* myIterator = std::min_element (TimeQ.begin(), TimeQ.end());&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">show_iterator[0] = *myIterator; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">for(int col=0; col&lt;TimeQ.size(); col++)&nbsp; {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">if(TimeQ[col] == *myIterator) { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">show_position[0]=col;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">break;&nbsp; }&nbsp;&nbsp; }&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <FONT FACE="Calibri">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">return show_position;&nbsp;&nbsp; // alternatively: return show_iterator; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">&nbsp;fun &lt;- cxxfunction(signature(arg1=&quot;numeric&quot;),src,plugin=&quot;Rcpp&quot;)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></P>

<P DIR=LTR><SPAN LANG="en-us">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">Times&lt;-c(1944.285,2920.969,1720.230,1264.438,3607.507,1720.230,25176.020);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </FONT></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"><FONT FACE="Calibri">fun_test&lt;- fun(Times)</FONT></SPAN><SPAN LANG="en-us"></SPAN></P>

<P DIR=LTR><SPAN LANG="en-us"></SPAN></P>

</BODY>
</HTML>