我很难理解如何使用Rcpp::XPtr
.
我想将整数向量的R列表传递给C++,将其转换为std::vector<std::unordered_set<int> >
,并将其作为Rcpp::XPtr
返回给R,然后在后面的C++函数中重复使用它.
// [[Rcpp::plugins(openmp)]]
#include <Rcpp.h>
#include <vector>
#include <unordered_set>
#include <omp.h>
#include <cstddef>
// [[Rcpp::export]]
Rcpp::XPtr<std::vector<std::unordered_set<int> > > convert_to_cpp_type(Rcpp::List& x) {
std::size_t x_size = x.size();
std::vector<std::unordered_set<int> > y(x_size);
for(std::size_t i = 0; i < x_size; ++i) {
Rcpp::IntegerVector x_i = x[i];
y[i].insert(x_i.begin(), x_i.end());
}
Rcpp::XPtr<std::vector<std::unordered_set<int> > > z(y);
return z;
}
// [[Rcpp::export]]
Rcpp::NumericVector use_xptr(SEXP a) {
Rcpp::XPtr<std::vector<std::unordered_set<int> > > b(a);
std::size_t b_size = (*b).size();
std::vector<double> c (b_size);
#pragma omp parallel for num_threads(10)
for(std::size_t i = 0; i < b_size; ++i) {
c[i] = example_function((*b)[i]);
}
return Rcpp::wrap(c);
}
这段代码不能编译,但它应该提供了我的目标是做什么的 idea .第二个函数中的a
是第一个函数导出的XPtr
.
此代码中应该有多个错误.然而,即使在浏览了数小时的Stack Overflow、RCPP网站和各种其他站点之后,我也不知道如何正确地实现它.