Data Structures

STL provides a large set of data structures. Some of the most important:

  • std::vector - like an R vector, except knows how to grow efficiently

  • std::unordered_set - unique set of values. Ordered version std::set. Unordered is more efficient.

  • std::map - Moslty similar to R lists, provide an association between a key and a value. There is also an unordered version.

A quick example illustrating the map:

#include <Rcpp.h>
using namespace Rcpp;

// [[Rcpp::export]]
std::map<double, int> tableC(NumericVector x) {
  // Note the types are <key, value>
  std::map<double, int> counts;

  int n = x.size();
  for (int i = 0; i < n; i++) {
    counts[x[i]]++;
  }

  return counts;
}
res = tableC(c(1,1,2,1,4,5))
res
#> 1 2 4 5 
#> 3 1 1 1
  • Note that the map is converted to a named vector in this case on return

To learn more about the STL data structures see containers at cppreference