title: selecting vector based on index in R vs rcpp
body: <p><strong>EDIT</strong>
  After doing some more reading around C++, I managed to concise my question on what exactly I want to do. Basically, I have two vectors:</p>
  <pre><code> my_vec &lt;- c(270, 291, 330, 378)
   x &lt;- 1:109 
  </code></pre>
  <p>The four elements of <code>my_vec</code> correspond to 1st, 23rd, 61st and 109th element of vector <code>x</code> I want to write an Rcpp code that sums up x from 1:23rd value, 24th to 61st value and 62nd to 109th value as shown below in R:</p>
  <pre><code> sum(x[1:22]) # 253
   sum(x[23:61]) # 1638
   sum(x[62:109]) # 4104
  </code></pre>
  <p>I wrote the following Rcpp code to this:</p>
  <pre><code> library(Rcpp)
  
   cppFunction('
  
   List sum_calc(NumericVector x,
                 NumericVector my_vec){
  
    int n = my_vec.size();  
  
    NumericVector sum_vec (n-1);
  
    double sum;
  
    int start_counter;
    int counter = 0;
    int start;
    int end;
  
    for(int i=0; i&lt;(n-1); ++i){
  
        start = my_vec[i];
        end = my_vec[i + 1];
  
        double x_sum = 0;
        start_counter = start;
  
        while(start_counter &lt;= end){      
              x_sum += x[counter];
              counter += 1;
              start_counter += 1;
        };
  
        sum_vec[i] = x_sum;
      };  
  
    return Rcpp::List::create(Rcpp::Named(&quot;sum_vec&quot;) = sum_vec);
  }
  ')
  
  
   sum_calc(x, my_vec)
  
   $sum_vec
  [1]  253 1700 4042
  </code></pre>
  <p>However only the first result is matching my output while the others two don't. I  think this is an index question but I don't know how to go about it. I have read C++ online and some material but still I cannot solve this. Where I am getting it wrong?</p>