I am looking for a container that maps to a double pointer to an object. However, each key is simply a doubling range that matches this object.
For example, there may be a pair of keys / values that are <(0,0 3,0), ptr> or <(3,5 10,0), ptr2>
container [1.0] should return ptr, container [3.0] should also return ptr, and container [-1.0] should be undefined.
Is there any object with similar default behavior or should I implement it myself?
Edit
Here's the real code I wrote, it may be easier to debug / offer advice on it.
// Behavior: A range is defined mathematically as (min, max] class dblRange { public: double min; double max; dblRange(double min, double max) { this->min = min; this->max = max; }; dblRange(double val) { this->min = val; this->max = val; }; int compare(const dblRange rhs) { // 1 if this > rhs // 0 if this == rhs //-1 if this < rhs if (rhs.min == rhs.max && min == max) { /*if (min > rhs.min) return 1; else if (min == rhs.min) return 0; else return -1;*/ throw "You should not be comparing values like this. :(\n"; } else if (rhs.max == rhs.min) { if (min > rhs.min) return 1; else if (min <= rhs.min && max > rhs.min) return 0; else // (max <= rhs.min) return -1; } else if (min == max) { if (min >= rhs.max) return 1; else if (min < rhs.max && min >= rhs.min) return 0; else // if (min < rhs.min return -1; } // Check if the two ranges are equal: if (rhs.min == min && rhs.max == max) { return 0; } else if (rhs.min < min && rhs.max <= min) { // This is what happens if rhs is fully lower than this one. return 1; } else if (rhs.min > min && rhs.min >= max) { return -1; } else { // This means there an undefined case. Ranges are overlapping, // so comparisons don't work quite nicely. throw "Ranges are overlapping weirdly. :(\n"; } }; int compare(const dblRange rhs) const { // 1 if this > rhs // 0 if this == rhs //-1 if this < rhs if (rhs.min == rhs.max && min == max) { /*if (min > rhs.min) return 1; else if (min == rhs.min) return 0; else return -1;*/ throw "You should not be comparing values like this. :(\n"; } else if (rhs.max == rhs.min) { if (min > rhs.min) return 1; else if (min <= rhs.min && max > rhs.min) return 0; else // (max <= rhs.min) return -1; } else if (min == max) { if (min >= rhs.max) return 1; else if (min < rhs.max && min >= rhs.min) return 0; else // if (min < rhs.min return -1; } // Check if the two ranges are equal: if (rhs.min == min && rhs.max == max) { return 0; } else if (rhs.min < min && rhs.max <= min) { // This is what happens if rhs is fully lower than this one. return 1; } else if (rhs.min > min && rhs.min >= max) { return -1; } else { // This means there an undefined case. Ranges are overlapping, // so comparisons don't work quite nicely. throw "Ranges are overlapping weirdly. :(\n"; } }; bool operator== (const dblRange rhs ) {return (*this).compare(rhs)==0;}; bool operator== (const dblRange rhs ) const {return (*this).compare(rhs)==0;}; bool operator!= (const dblRange rhs ) {return (*this).compare(rhs)!=0;}; bool operator!= (const dblRange rhs ) const {return (*this).compare(rhs)!=0;}; bool operator< (const dblRange rhs ) {return (*this).compare(rhs)<0;}; bool operator< (const dblRange rhs ) const {return (*this).compare(rhs)<0;}; bool operator> (const dblRange rhs ) {return (*this).compare(rhs)>0;}; bool operator> (const dblRange rhs ) const {return (*this).compare(rhs)>0;}; bool operator<= (const dblRange rhs ) {return (*this).compare(rhs)<=0;}; bool operator<= (const dblRange rhs ) const {return (*this).compare(rhs)<=0;}; bool operator>= (const dblRange rhs ) {return (*this).compare(rhs)>=0;}; bool operator>= (const dblRange rhs ) const {return (*this).compare(rhs)>=0;}; };
Now I have a problem with the fact that the card accepts a double key, even if comparison operators are defined.
Here is some code that I use to check if it will work:
std::map<dblRange, int> map; map[dblRange(0,1)] = 1; map[dblRange(1,4)] = 2; map[dblRange(4,5)] = 3; map[3.0] = 4;