我刚开始接触Rcpp和C++编程,所以请原谅我问一些基础问题。代码中的cpp部分如下:
// test.cpp
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
void testRun() {
IntegerVector Grp1 = IntegerVector::create(1,2,3,4);
IntegerVector Grp2 = IntegerVector::create(3,4,5,6);
Rf_PrintValue(Grp1);
Grp1 = Grp2;
Rf_PrintValue(Grp1);
Grp2[3] = Grp2[3]+1;
Rf_PrintValue(Grp1);
}
当我运行testrun()函数时,会得到输出结果。
> Rcpp::sourceCpp('test.cpp')
> testRun()
[1] 1 2 3 4
[1] 3 4 5 6
[1] 3 4 5 7
当我将
Gr2
分配给Gr1
,即使用Gr1 = Gr2
时,改变Gr2
中的元素会同时改变Gr1
中的值。是否有一个IntegerVectors函数可以执行类似于Gr1 = Gr2的副本
的操作,或者我应该使用循环来完成呢?谢谢!
Rcpp::clone()
,就像这个回答中所提到的 http://stackoverflow.com/a/21282929/143305 以及其他回答? - Dirk Eddelbuettel