I feel this is probably an elementary question, but I can't find a simple answer after quite a bit of searching, so I thought I'd ask.
I have a function that is meant to return the nth percentile value in a container, but for legacy reasons the array can be either a vector or valarray, and it can contain doubles or floats. What is the correct syntax for the function? At the moment I have:
template <template <class> class vType, class elType>
elType GetPercentile(vType<elType>& vData, double dPercentile)
{
int iOffset = int(dPercentile * vData.size());
std::nth_element(begin(vData), begin(vData) + iOffset, end(vData));
return static_cast<elType>(vData[iOffset]);
}
This compiles OK when passing a valarray, but fails for a vector:
'elType GetPercentile(vType &,double)': could not deduce template argument for 'vType &' from 'std::vector<float,std::allocator>'
Is there a way of doing this? It seems silly to duplicate the code for the two container types. (And if there are any comments on the code itself, that would be fine too.)
Many thanks for any advice. Bill H