我試圖運行下面的代碼。坦率地說,我只知道C++很少,但我想要得到以下函數運行。你能幫我運行這個愚蠢的例子嗎?使用冒號(':')在C++中訪問數組中的元素(在Rcpp中)
cppFunction(
'NumericVector abc(int x, int x_end, NumericVector y)
{
NumericVector z;
int x1 = x + x_end;
z = y[x:x1];
return(z);
}'
)
abc(3,c(0,1,10,100,1000,10000))
我看到這個...
錯誤:之前預計 ']' ':' 令牌
更新 對不起,我忘了提,我需要生成一個序列的數字從x
到x1
。函數IntegerVector::create
僅創建一個變量,只有x
和x1
而不是x
,儘管x1
。我給的例子是微不足道的。我現在更新了這個例子。我需要在C++什麼seq()
在[R確實基於以下(@SleuthEye)
Rcpp::cppFunction(
'NumericVector abc(int x, int x_end, NumericVector y)
{
NumericVector z;
Range idx(x,x_end);
z = y[idx];
return(z);
}'
)
abc(3,5,c(0,1,10,100,1000,10000))
[1] 100 1000 10000
我不是太熟悉RCPP,但[此鏈接](http://gallery.rcpp.org/articles/subsetting/)表明它可以與像'IntegerVector IDX完成= IntegerVec TOR ::創建(X,X 1); z = y [idx];' – SleuthEye
這也是一個有趣的閱讀:[埃裏克Niebler在C++中的一片Python](http://ericniebler.com/2014/12/07/a-slice-of-python- in-c /) – nonsensation