[Rcpp-devel] What is the most efficient method to check if a Rcpp vector contains an element?

Romain Francois romain at r-enthusiasts.com
Sun Dec 29 13:14:05 CET 2013


Le 29 déc. 2013 à 12:50, Asis Hallab <asis.hallab at gmail.com> a écrit :

> Dear Rcpp experst,
> 
> I hope everyone has had a pleasant Xmas.
> 
> I am just wondering what would be the recommended and most efficient way to check, if a Rcpp Vector contains a given element?
> 
> 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:
> 
> http://stackoverflow.com/questions/571394/how-to-find-an-item-in-a-stdvector

I believe you are mistaken. sorting is an expensive O( N log(N) ) operation. 

I’d use something like std::find (or std::any_of if you use C++11). Those are O(N)
Otherwise, you might like Rcpp::any or Rcpp::any_of

Romain

> What would be the Rcpp solution for the same task on a Rcpp vector? 
> Should I convert my Rcpp Vector to a std::vector and do the above, as explained on the stackoverflow blog?
> 
> Cheers and a happy new year!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.r-forge.r-project.org/pipermail/rcpp-devel/attachments/20131229/44ba6195/attachment.html>


More information about the Rcpp-devel mailing list