Consider the problem to sort all elements of the given vector in ascending order. We can simply use the function std::sort from the C++ STL. #include <Rcpp.h> using namespace Rcpp; // ] NumericVector stl_sort(NumericVector x) { NumericVector y = clone(x); std::sort(y.begin(), y.end()); return y; } library(rbenchmark) set.seed(123) z <- rnorm(100000) x <- rnorm(100) # check that stl_sort is the same as sort stopifnot(all.equal(stl_sort(x), sort(x))) #...