Rcpp 对象的有效转换列表

问题描述 投票:0回答:1

有谁知道 Rcpp 对象(例如

IntegerVector
)和常见的 C 或 C++ 等效项之间是否存在有效转换列表?

原因是我有很多 C 代码,我希望能够与 Rcpp 集成,但我真的不想在将向量转换为 Rcpp 对象时放置大量循环。如果有一个更简单的解决方案,那么我也很想听到它,尽管有效转换列表的问题是我之前想知道的。

r c casting rcpp
1个回答
0
投票

当您要求 casts (编译器/语言提供的,即从

int
double
等珍贵的向上转换)时,我怀疑您要求 Rcpp 称为
as<>()
wrap()
的转换助手。

众所周知,R 本身仅使用

integer
(或 C++ 中的
int
int64_t
)和
double
又名
numeric
,在 C++ 中也是
double
。这些直接工作(见下文),对于其他人来说有点模糊,因为 R
' 
逻辑
 is actually three-values (
TRUE
or
FALSE
or
NA
!!) so it uses an
integer` 在引擎盖下。

> Rcpp::cppFunction("int mysum(std::vector<int> sv) { \  # really one line
         IntegerVector iv = as<IntegerVector>(wrap(sv)); return(sum(iv)); }")
> mysum(c(3L, 39L))
[1] 42
>

反之亦然:

> Rcpp::cppFunction("std::vector<double> minusone(NumericVector nv) \  # one line
        { NumericVector v = nv - 1; return(as<std::vector<double>>(v));}") 
> minusone(c(10.5, 27.28)) 
[1]  9.50 26.28
> 

我不确定是否存在所有转换器的“列表”。有些隐藏在包的标头中,有些则位于附加包中。

© www.soinside.com 2019 - 2024. All rights reserved.