Data Structures
STL provides a large set of data structures. Some of the most important:
std::vector
- like anR
vector, except knows how to grow efficientlystd::unordered_set
- unique set of values. Ordered versionstd::set
. Unordered is more efficient.std::map
- Moslty similar toR
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;
}
- 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