Using a colon (':') to access elements in an array in C ++ (in Rcpp)

I am trying to run the following code. Honestly, I only know C ++, but I want to get the following function. Can you help me run this stupid example?

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))

I see it...

error: expected ']' before ':' token

Update Sorry, I forgot to mention that I need to generate a sequence of numbers from xto x1. The function IntegerVector::createonly creates a variable with xand x1not x, though x1. The example I gave was trivial. Now I updated this example. I need to do in C ++ what seq()does in R

Solution based on answer below (@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
+4
1

Rcpp cppFunction ++. , - ++. , ++ (:), , , ++ , ( [] :). int IntegerVector, :.

Rcpp, , (x,x+1), index NumericVector :

IntegerVector idx = IntegerVector::create(x, x+1);
z = y[idx];

Range :

Range idx(x, x1);
z = y[idx];
+6

All Articles