I have to calculate the modal value of
std::vector< int > quite often, so I thought I'd share my general method for people to look at, use and/or comment. In my case, the vectors usually contain values in the range 0 - 255.
The code first creates a
std::vector< int > of random integers, to use as sample data. Here, I'm using the
std::transform() function to populate a vector with random values, via a simple function that takes one argument that determines in what range the random number will be.
std::vector< int > is made that will hold a histogram of the values in the original vector, then values in this vector are initialised to
0 . The histogram is populated by the loop on line 22. The mode of the data is found by using
std::max_element() to find the most populated bin in the histogram.
std::max_element() returns an iterator to the element with the maximum value in it, we can find which bins this is by subtracting
histogram.begin() from it.
This approach works well when the range of the integers in the vector is quite small, since the memory taken by the histogram vector is proportional to this. If the range is large and/or sparse a
std::map< int,int > can be used to store the histogram instead of a
std::vector . This saves memory, but finding the maximum bin has to be done manually. (unless someone has a good STL-ish way to do this?)
Anyway, hope that's useful to someone