Skip to content

[Rcpp-devel] Feature request: const_iterators

2 messages · schattenpflanze at arcor.de, Romain Francois

#
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
#
Le 11/04/11 12:30, schattenpflanze at arcor.de a ?crit :
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