<html><head><meta http-equiv="Content-Type" content="text/html charset=windows-1252"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><br><div><div>Le 29 déc. 2013 à 12:50, Asis Hallab <<a href="mailto:asis.hallab@gmail.com">asis.hallab@gmail.com</a>> a écrit :</div><br class="Apple-interchange-newline"><blockquote type="cite"><div dir="ltr">Dear Rcpp experst,<div><br></div><div>I hope everyone has had a pleasant Xmas.</div><div><br></div><div>I am just wondering what would be the recommended and most efficient way to check, if a Rcpp Vector contains a given element?</div>
<div><br></div><div>If I am not mistaken the C++ standard approach for very large std::vectors would be to first sort them and then perform a binary search:</div><div><br></div><div><a href="http://stackoverflow.com/questions/571394/how-to-find-an-item-in-a-stdvector">http://stackoverflow.com/questions/571394/how-to-find-an-item-in-a-stdvector</a></div></div></blockquote><div><br></div><div>I believe you are mistaken. sorting is an expensive O( N log(N) ) operation. </div><div><br></div><div>I’d use something like std::find (or std::any_of if you use C++11). Those are O(N)</div><div>Otherwise, you might like Rcpp::any or Rcpp::any_of</div><div><br></div><div>Romain</div><div><br></div><blockquote type="cite"><div dir="ltr"><div>What would be the Rcpp solution for the same task on a Rcpp vector? </div><div>Should I convert my Rcpp Vector to a std::vector and do the above, as explained on the stackoverflow blog?</div><div>
<br></div><div>Cheers and a happy new year!</div></div></blockquote></div></body></html>