我很难理解如何使用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);
}
字符串
这段代码不能编译,但它应该提供了我的目标是做什么的想法。第二个函数中的a
是第一个函数导出的XPtr
。
在这段代码中应该有多个错误。然而,即使在浏览了几个小时的Stack Overflow,Rcpp网站和其他各种网站之后,我也没有弄清楚如何正确实现这一点。
1条答案
按热度按时间qv7cva1a1#
你的第一个函数不能编译,因为XPtr构造函数需要一个原始指针,而不是一个
std::vector
。然而,返回的指针将比无用更糟糕,因为它指向一个在函数返回后超出范围的局部变量。如果你稍后尝试使用它,那么你的会话将崩溃。下面的函数将返回一个指向
std::vector<std::unordered_set>>
的有效指针:字符串
为了得到指针所索引的对象的内容,我们需要再次构建一个R列表:
型
为了完整起见,让我们创建一个函数,我们可以从R调用它来修改我们的C++对象(因为我们没有
example_function
型
在R中,我们可以做:
型
这似乎是一个不必要的困难的方式来做的事情.在我看来,更有意义的是发送一个列表到C++,让它做你想要的所有计算,并返回结果.指针并没有真正帮助你在这里.