Open IndrajeetPatil opened 2 years ago
Here is another example from the same section Q3:
// As a one-liner
// [[Rcpp::export]]
std::unordered_set<double> uniqueCC(NumericVector x) {
return std::unordered_set<double>(x.begin(), x.end());
}
The outputs are different:
v1 <- c(1, 3, 3, 6, 7, 8, 9)
unique(v1)
#> [1] 1 3 6 7 8 9
uniqueCC(v1)
#> [1] 9 8 1 7 3 6
Do you have an example with a differing result, eg a differing set? It seems that just the order is different.
(That said, I see that it can make a huge difference in code as I rely on the order of unique (ie first appearance) quite often in real world code)
No, but I thought the exercises expect these results to be the same (they are indeed the same for all other Rcpp chapter exercises, except the ones with STL).
Otherwise, performance of R and C++ functions can't be compared, since they are producing different outputs.
I agree. Thanks for the additional thoughts. Ist makes sense to fix this some time or at least leave a note in the answer. Also edited my comment above.
For example, in section 20.3 Q6, the following code creates a version of
union()
in C++:But it doesn't produce the same output as its R equivalent: