[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

R> ##prints 6 and 5

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++) {

  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
##prints 6 and 5

We'll check end().


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

More information about the Rcpp-devel mailing list