I’m looking for a class that categorizes floating point numbers into arbitrary bins. The bins. The desired syntax would be something like:
std::vector<double> bin_vector;
// ..... fill the vector with 1, 1.4, 5, etc not evenly spaced values
Binner bins(bin_vector);
for (std::vector<double>::const_iterator d_itr = some_vector.begin();
d_itr != some_vector.end(); d_itr++) {
int bin = bins.categorize(*d_itr);
// bin would be 0 for x < 1, 1 for 1 < x < 1.4, etc
// do something with bin
}
Unfortunately, due to portability requirements I’m limited to boost and stl. I’ve rolled my own O(log n) solutions using maps and overloading < for a custom range object, but that solution seemed bug prone and ugly at best.
Is there some simple stl or boost object solution to this?
Use a std::map, mapping interval boundaries to bin numbers. Then use .upper_bound() to find the bin.