Re: Looking for container like std::map but for ranges
Jerry Coffin wrote:
The container just uses the comparison you define for it. To work with
ranges like this, all values within a range will be considered equal.
#include <map>
#include <iostream>
#include <string>
struct cmp {
int range(unsigned value) const {
static const unsigned bounds[] = { 11, 50001, 60000};
for (int i=0; i<3; i++)
if (value < bounds[i])
return i;
return -1;
}
public:
bool operator()(unsigned a, unsigned b) const {
return range(a) < range(b);
}
};
int main() {
std::map<unsigned, std::string, cmp> values;
values[5] = "String 1";
values[20] = "string 2";
values[55000] = "String 3";
std::cout << "Values[10] = " << values[10] << "\n";
return 0;
}
Uh, okay, maybe I should have specified this, but I was looking for a
container that would let me specify, inspect and redefine/move the range
bounds at runtime.