| Stewbond (1843) | |||||
|
I've been trying to make a range-based function for a statistics library, but I'm having trouble making it a true template function. This works (if I insert std::vector<int>::iterator as the arguments)
This works for pre-C++0x arrays:
However, I want something that will work with any STL (or custom) container with iterators and any array (pointer). Does anyone have any ideas? | |||||
|
|
|||||
| Cubbi (1927) | |||
Time to learn about std::iterator_traits
but in this particular case, you could have just called std::accumulate
| |||
|
Last edited on
|
|||
| vlad from moscow (3662) | |
|
Use std::accumulate algorithm declared in <numeric>. All is done already before you!:) | |
|
|
|
| Stewbond (1843) | |
|
Thanks guys! sum() was the most basic function that I'm doing. I wanted to do mean, median, mode, standard deviation, min, max, variance, correlations, entropy and skewness. I checked out the possible implementation of std::accumulate from here: http://cplusplus.com/reference/numeric/accumulate/ And it makes sense! What I was missing was template type for the init value. Of course, I'd rather not make people specify initial values just for the type, so std::iterator_traits looks nifty. | |
|
|
|
| Cubbi (1927) | ||
There's a library for that: http://www.boost.org/doc/libs/release/doc/html/accumulators.html | ||
|
|
||