[Rcpp-devel] R.e. is std::sort function broken??

Darren Cook darren at dcook.org
Thu Sep 22 13:59:55 CEST 2011


> I think this is the current prize-winner of inappropriate use of Rcpp...
> How about using R to sort your vector?!

There is also the fact that the array is being created ready-sorted :-).
I assumed this was an artificially simplified example from real code. Or
a learning toy.

> This isn't really an Rcpp question, but since you asked, .push_back()
> is a great way to slow yourself down.

Or add data.reserve(20) [1] after creating the vector. Then push_back()
is very quick.

BTW, apologies for replying to something off-topic. Perhaps, Noah, Stack
Overflow would be a better place for pure-C++ questions? (Any other
suggestions anyone?)

Darren

[1]: http://www.cplusplus.com/reference/stl/vector/reserve/

>>                std::vector<double> data;
>>
>>                for(int i=0; i != 20; i++){
>>                        data.push_back(i);
>>                }
>>                std::sort(data.front(), data.back());
>>                return Rcpp::wrap(data);



-- 
Darren Cook, Software Researcher/Developer

http://dcook.org/work/ (About me and my work)
http://dcook.org/blogs.html (My blogs and articles)


More information about the Rcpp-devel mailing list