Get startedGet started for free

Vectors from the STL

1. Vectors from the STL

Up to this point, you have only used data structures from Rcpp.

2. Rcpp vectors vs STL vectors

This is the most common situation because Rcpp vectors are designed as very thin wrappers around their R data structures, and therefore it is easy and cheap to move data between the R and C++ worlds. This, however, has drawbacks. The main disadvantage of a Rcpp vector is resizing. When you add a value to a vector or remove a value from a vector, you trigger an entire copy of the memory of the vector. Most of the time, that is not a problem, you just have to know the final size before filling the vector. Alternatively, you can use vectors from the standard template library, "STL". Vectors from the "STL" have the advantage of being able to grow or shrink without involving a full copy every time. The copy is amortized.

3. Extract positives values from a vector

The "extract_positives" R function is a straightforward way to extract positive values from a vector. It is conceptually easy to turn this iterating code like the "extract_positives_loop" function. Growing Rcpp vectors is just as expensive as growing R vectors, so let's just not look into that.

4. Extract positive values: alternative algorithm

But we have a problem, we don't know in advance the number of positive values in our final vector. To solve that problem, we can process the vector using two loops. The first loop scans the vector to count the number of positive values.

5. Extract positive values: alternative algorithm

Then we create a vector of the right final size. Finally, in the second loop, we fill the vector with the positive values we find. To do this we have to maintain two indices, one for the input vector that has both positive and negative values and one for the result vector that only has positive values. As a consequence, the loop is slightly more complicated, but it is the price to pay to avoid expensive data copies.

6. Simpler algorithm using the STL

The standard template library gives us the "std::vector" class. The std vector class contains many vectors that are beyond the scope of this course, the interesting method for our current example is the "push_back()" method. What this does is pushes the current value to the end of the vector. This is exactly what we want here. The cost of adding values is amortized, you don't pay a full copy every time. Let's try both versions and see what happens.

7. Let's practice!

Time to put this into practice.