[Rcpp-devel] Possible inconsistency in Rcpp vs stl in erase(iterator, iterator)

Dirk Eddelbuettel edd at debian.org
Wed Jun 5 14:41:39 CEST 2013


Hi Toni,


Nice example -- we'll take a look.  Probably a buglet at our end.  The STL
are just for convenience: each operation requires _a full copy_ underneath
(as we work on R memory) so they are hogs, which is why nobody may have found
this bug yet.

Two minor comments:  The BEGIN and END macros get autoinserted, you probably
want a return value, and the R code at the bottom get run when you call it:

R> sourceCpp("/tmp/toni.cpp")

R> truncateTest()
std::vector left with 6
Rcpp::List  left with 5
NULL

R> ##prints 6 and 5
R> 


With that, a minor improvement to your code:

#include <Rcpp.h>
#include <iostream>

// [[Rcpp::export]]
SEXP truncateTest() {

  using namespace std;
  Rcpp::List l;
  std::vector<int> v;

  for (int i=1; i<=10; i++) {
    v.push_back(i);
    l.push_back(i);
  }

  v.erase(v.begin()+5,v.end()-1);
  l.erase(l.begin()+5,l.end()-1); // ?

  cout << "std::vector left with " << v.size() << endl;
  cout << "Rcpp::List  left with " << l.size() << endl;

  return R_NilValue;
}

/*** R
truncateTest()
##prints 6 and 5
*/



We'll check end().

Dirk

-- 
Dirk Eddelbuettel | edd at debian.org | http://dirk.eddelbuettel.com


More information about the Rcpp-devel mailing list