Dirk Eddelbuettel — written Dec 21, 2012 — source
This example was motivated by http://stackoverflow.com/questions/5158219/find-minimum-of-vector-in-rcpp and addresses how to find the minumum value and its position index in a vector.
We can also use the iterator to compute the position, simply by taking the offset to the vector beginning.
A quick illustration follows. Note that we pad the position by one to adjust for the 0-based versus 1-based indexing between C++ and R.
[1] 21 68 90 28 11 67 50 76 88 96
Min is 11 and at position 5
Of course, we subsequently added min
and which_min
as sugar
functions, but this example still illustrated how useful the STL
algoritms can be.
tags: stl
Tweet