[Rcpp-devel] Feature request: const_iterators

Romain Francois romain at r-enthusiasts.com
Mon Apr 11 12:42:10 CEST 2011


Le 11/04/11 12:30, schattenpflanze at arcor.de a écrit :
> Hello,
>
> I have a suggestion for an additional feature of Rcpp. It would be nice
> to have const_iterators besides mutable iterators for
> Rcpp::NumericVector (and other vectors and matrices). This would make it
> easier to use templated functions like the one below, which can be used
> with std::vectors _and_ Rcpp::NumericVectors:
>
> template <typename Vector>
> void foo(const Vector& v) {
> typename Vector::const_iterator v_it=v.begin(), v_end=v.end();
> for (; v_it!=v_end; ++v_it) {
> // do amazing stuff
> }
> }
>
> Best regards,
> Peter

Hi,

Feel free to poke into the code and submit a tested patch.


Alternatively, you can do something like this:

template <typename Vector>
struct my_iterator{
	typedef typename Vector::const_iterator type ;
} ;
template <>
struct my_iterator<Rcpp::NumericVector>{
	typedef Rcpp::NumericVector::iterator type ;
} ;


template <typename Vector>
void foo(const Vector& v) {
  typename my_iterator<Vector>::type v_it=v.begin(), v_end=v.end();
  for (; v_it!=v_end; ++v_it) {
    // do amazing stuff
  }
}

Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
http://romain-francois.com
|- http://bit.ly/fhqbRC : Rcpp workshop in Chicago on April 28th
|- http://bit.ly/dFyZGB : Hydraulique au Montpellier Comedie Club
`- http://bit.ly/eVXit9 : Eponyme : 40 minutes stand up




More information about the Rcpp-devel mailing list