在Rcpp中声明一个变量作为参考 [英] Declare a variable as a reference in Rcpp
问题描述
在C ++中,我们可以声明一个变量作为引用.
In C++, we can declare a variable as a reference.
int a = 10;
int& b = a;
如果我们设置b=15
,a
也会更改.
If we set b=15
, a
also changes.
我想在Rcpp中做类似的事情.
I want to do the similar thing in Rcpp.
List X = obj_from_R["X"];
IntegerVector x_i = X[index];
x_i = value;
我想通过向其向量之一插入一个值来从R中更新名为X
的对象.上面的代码无法正常工作,因此我尝试了以下方法:
I want to update an object from R called X
by inserting a value to one of its vector. The code above did not work, so I tried this:
IntegerVector& x_i = X[index];
并收到错误.
error: non-const lvalue reference to type 'IntegerVector'
(aka 'Vector<13>') cannot bind to a temporary of type 'Proxy' (aka 'generic_proxy<19>')
推荐答案
这个问题在不同的变体中被问了很多.
This question gets asked a lot in differing variants.
以下是一些受欢迎的答案:
Here are some popular answers:
- 在C ++函数内,如何将Rcpp对象传递给其他功能(通过引用或复制)?
- 更新通过引用使用RcppArmadillo submat()通过引用传递的Rcpp :: NumericMatrix
- Rcpp通过引用与按值传递
- 通过引用传递data.frame并使用rcpp更新它
- Rcpp更新矩阵引用并返回R 中的更新
- 将大型矩阵传递给RcppArmadillo函数而不创建副本(高级构造函数)
- Rcpp:代理模型的行为不一致
- 为什么Rcpp实现用于查找唯一项目的数量比基本R慢吗?
- Within C++ functions, how are Rcpp objects passed to other functions (by reference or by copy)?
- Update Rcpp::NumericMatrix passed by reference using RcppArmadillo submat()
- Rcpp pass by reference vs. by value
- Passing by reference a data.frame and updating it with rcpp
- Rcpp Update matrix passed by reference and return the update in R
- Passing large matrices to RcppArmadillo function without creating copy (advanced constructors)
- Rcpp: Inconsistent behavior with proxy model
- Why is my Rcpp implementation for finding the number of unique items slower than base R?
更多详细信息...
More details...
从FAQ条目 Rcpp更改了我按值传递的(const)对象 :
From the FAQ entry Rcpp changed the (const) object I passed by value that I wrote:
Rcpp 对象是基础 R 对象的SEXP或S表达式的包装.
SEXP
是一个指针变量,用于保存 R 对象数据 R:Internals 的存储位置.也就是说,SEXP
不保存 R 对象的实际数据,而仅保存数据所在的引用.为 R 对象创建新的 Rcpp 对象以输入 C ++ 时,该对象将使用与原始 R相同的SEXP 对象(如果类型匹配),否则必须创建新的SEXP以确保类型安全.本质上,底层SEXP对象是通过引用传递的,而没有显式复制到 C ++ 中.我们将此安排称为代理模型.
Rcpp objects are wrappers around the underlying R objects' SEXP, or S-expression. The
SEXP
is a pointer variable that holds the location of where the R object data has been stored R:Internals. That is to say, theSEXP
does not hold the actual data of the R object but merely a reference to where the data resides. When creating a new Rcpp object for an R object to enter C++, this object will use the same SEXP that powers the original R object if the types match otherwise a new SEXP must be created to be type safe. In essence, the underlying SEXP objects are passed by reference without explicit copies being made into C++. We refer to this arrangement as a proxy model.
因此,&
只是视觉上的糖,因为 Rcpp 对象已经充当了引用.
So, the &
is just visual sugar as Rcpp objects already act as references.
因此,以下内容将显示结论:
Thus, the following would show the conclusion:
#include <Rcpp.h>
// [[Rcpp::export]]
void show_references(Rcpp::List X,
Rcpp::IntegerVector y,
int index = 0) {
X[index] = y;
}
示例:
y_vec = c(-1L, 8L, 12L)
X_list = list(a = c(0L, 2L, 3L), b = c(42L, 50L, 30L))
X_list
# $a
# [1] 0 2 3
#
# $b
# [1] 42 50 30
show_references(X_list, y_vec)
X_list
# $a
# [1] -1 8 12
#
# $b
# [1] 42 50 30
我创建的以下 Rcpp 代理模型幻灯片应进一步说明正在发生的情况
The following Rcpp proxy model slides I created should further illustrate what is happening
来源: https://twitter.com/axiomsofxyz/status/938881541396197377
这篇关于在Rcpp中声明一个变量作为参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!