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
[Rcpp-devel] Feature request: const_iterators
2 messages · schattenpflanze at arcor.de, Romain Francois
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