Point taken: STL looks like the way to go in general. In my particular example, however, the arrays get immediately cast to another structure foo (somebody else's code). So I need to cast from IntegerVector to int[] before they get cast to foo[]. What you suggested works perfectly (and makes sense in hindsight).
Is the story identical for char[]? Where x is a CharacterVector, I tried char* a = x.begin(); and got error: cannot convert ‘Rcpp::Vector<16>::iterator’ to ‘char*’ in assignment Any help much appreciated. Chris On Fri, Aug 12, 2011 at 3:19 PM, Dirk Eddelbuettel <[email protected]> wrote: > > On 12 August 2011 at 14:50, Chris DuBois wrote: > | Hi all, > | > | I'm trying to figure out how to pass in an array of integers to a > function > | inside a module. For example, adding the following function to > runit.Module.R > | works fine: > | > | int bla3( IntegerVector x ) { > | return sum(x); > | } > | > | However, I need to pass an int array, rather than an IntegerVector. > Using int > | x[] in the arguments doesn't compile (though I'm unfamiliar with C++ in > | general, so maybe this shouldn't work anyway). > | > | Alternatively, should I just cast x from an IntegerVector to an int > array? I > | tried various permutations of as, vector, <int>, etc, and would like to > learn > | the proper way of doing this. > > You generally do not want old school x[] arrays in C++. Why? Because STL > vectors do _everything_ they do at (essentially) zero added cost, free you > from malloc/free and still allow you to access the straight memory should > you > need to (to talk to a C API, say). > > So use IntegerVector for _the interface_. You can the, if you must, do > > IntegerVector x; > > int a1[] = x.begin(); // STL-style iterator to beginning of memory > int *a2 = x.begin(); // idem > > Hope this helps, Dirk > > -- > Two new Rcpp master classes for R and C++ integration scheduled for > New York (Sep 24) and San Francisco (Oct 8), more details are at > > http://dirk.eddelbuettel.com/blog/2011/08/04#rcpp_classes_2011-09_and_2011-10 > > http://www.revolutionanalytics.com/products/training/public/rcpp-master-class.php >
_______________________________________________ Rcpp-devel mailing list [email protected] https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/rcpp-devel
