Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ STL Range Container
    text
    copied!<p>I'm looking for a container that maps from a double to object pointers. However, each key is simply a range of doubles that would correspond to that object.</p> <p>For example, there could be a key/value pair that's &lt;(0.0 3.0), ptr>, or &lt;(3.5 10.0), ptr2></p> <p>container[1.0] should return ptr, container[3.0] should also return ptr, and container[-1.0] should be undefined.</p> <p>Is there any object with similar behaviour by default or will I have to implement it myself?</p> <p><strong>Edit</strong></p> <p>Here's the actual code that I've written, it might be easier to debug/offer advice on it. </p> <pre><code>// Behavior: A range is defined mathematically as (min, max] class dblRange { public: double min; double max; dblRange(double min, double max) { this-&gt;min = min; this-&gt;max = max; }; dblRange(double val) { this-&gt;min = val; this-&gt;max = val; }; int compare(const dblRange rhs) { // 1 if this &gt; rhs // 0 if this == rhs //-1 if this &lt; rhs if (rhs.min == rhs.max &amp;&amp; min == max) { /*if (min &gt; 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 &gt; rhs.min) return 1; else if (min &lt;= rhs.min &amp;&amp; max &gt; rhs.min) return 0; else // (max &lt;= rhs.min) return -1; } else if (min == max) { if (min &gt;= rhs.max) return 1; else if (min &lt; rhs.max &amp;&amp; min &gt;= rhs.min) return 0; else // if (min &lt; rhs.min return -1; } // Check if the two ranges are equal: if (rhs.min == min &amp;&amp; rhs.max == max) { return 0; } else if (rhs.min &lt; min &amp;&amp; rhs.max &lt;= min) { // This is what happens if rhs is fully lower than this one. return 1; } else if (rhs.min &gt; min &amp;&amp; rhs.min &gt;= max) { return -1; } else { // This means there's 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 &gt; rhs // 0 if this == rhs //-1 if this &lt; rhs if (rhs.min == rhs.max &amp;&amp; min == max) { /*if (min &gt; 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 &gt; rhs.min) return 1; else if (min &lt;= rhs.min &amp;&amp; max &gt; rhs.min) return 0; else // (max &lt;= rhs.min) return -1; } else if (min == max) { if (min &gt;= rhs.max) return 1; else if (min &lt; rhs.max &amp;&amp; min &gt;= rhs.min) return 0; else // if (min &lt; rhs.min return -1; } // Check if the two ranges are equal: if (rhs.min == min &amp;&amp; rhs.max == max) { return 0; } else if (rhs.min &lt; min &amp;&amp; rhs.max &lt;= min) { // This is what happens if rhs is fully lower than this one. return 1; } else if (rhs.min &gt; min &amp;&amp; rhs.min &gt;= max) { return -1; } else { // This means there's 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&lt; (const dblRange rhs ) {return (*this).compare(rhs)&lt;0;}; bool operator&lt; (const dblRange rhs ) const {return (*this).compare(rhs)&lt;0;}; bool operator&gt; (const dblRange rhs ) {return (*this).compare(rhs)&gt;0;}; bool operator&gt; (const dblRange rhs ) const {return (*this).compare(rhs)&gt;0;}; bool operator&lt;= (const dblRange rhs ) {return (*this).compare(rhs)&lt;=0;}; bool operator&lt;= (const dblRange rhs ) const {return (*this).compare(rhs)&lt;=0;}; bool operator&gt;= (const dblRange rhs ) {return (*this).compare(rhs)&gt;=0;}; bool operator&gt;= (const dblRange rhs ) const {return (*this).compare(rhs)&gt;=0;}; }; </code></pre> <p>Right now I'm having trouble having the map accept a double as a key, even though the comparison operators are defined.</p> <p>Here's some driving code that I'm using to test if it would work:</p> <pre><code>std::map&lt;dblRange, int&gt; map; map[dblRange(0,1)] = 1; map[dblRange(1,4)] = 2; map[dblRange(4,5)] = 3; map[3.0] = 4; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload