将 rcpp 变量转换为标准 C++ 变量 [英] Convert rcpp variables into standard C++ variables

查看:39
本文介绍了将 rcpp 变量转换为标准 C++ 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的输入是这样的:

Rcpp::NumericMatrix data(dataMatrix);
Rcpp::NumericVector xSize(dataXsize);
Rcpp::NumericVector ySize(dataYsize);
Rcpp::NumericVector tIndexes(testIndexes);
Rcpp::NumericVector cIndexes(controlIndexes);

我试图调用的外部库有这个签名

And the external library I'm trying to call has this signature

WilcoxonTest(float * _data, int _dataXsize, int _dataYsize, vector<int> * _testIndexes, vector<int> * _controlIndexes);

如何将 Rcpp 数据类型转换为 C++ 标准数据类型?

How can I convert the Rcpp data types into C++ standard data types?

注意:

 float * _data

是一个浮点值矩阵.图书馆假设它是这样的:

Is a matrix of floating point values. The library assumes its something like this:

 float * _data = new float[dataXsize * dataYsize];

推荐答案

要从 NumericMatrix 中获取 std::vector,您可以使用:

To get a std::vector<float> out of your NumericMatrix, you can use:

std::vector<float> dat = Rcpp::as<std::vector<float> >(data);

这个向量中的数字将被存储为它们在 R 中的矩阵中的存储(例如 matrix(1:4, nrow=2) 将产生一个向量 1 2 34).如果您需要转置,我建议从 R 中调用 t.

The numbers in this vector will be stored as they are stored in the matrix in R (e.g. matrix(1:4, nrow=2) will result in a vector 1 2 3 4). If you need the transpose, I would suggest calling t from within R.

您的问题剩下的就是将 vector 转换为 C 风格的原始指针.这可以通过以下方式完成:

All that remains for your problem is converting the vector into a C-style raw pointer. This can be done with:

float* _data = &dat[0];

关于从 std::vector 对象中获取原始向量的一些警告:如果您后来将对象添加到 dat 或删除了 dat_data 指针可能会失效.

Some words of warning about grabbing raw vectors out of std::vector objects: if you later added objects to dat or deleted dat, the _data pointer could become invalid.

您可以通过类似使用 Rcpp::as 函数获取其他参数:

You can grab the other parameters through similar use of the Rcpp::as function:

int _dataXsize = Rcpp::as<int>(xSize);
int _dataYsize = Rcpp::as<int>(ySize);
std::vector<int> _testIndexes = Rcpp::as<std::vector<int> >(tIndexes);
std::vector<int> _controlIndexes = Rcpp::as<std::vector<int> >(cIndexes);

这篇关于将 rcpp 变量转换为标准 C++ 变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆