Note that there are some explanatory texts on larger screens.

plurals
  1. POusing STL Container upper_bound & lower_bound for map
    primarykey
    data
    text
    <p>I have the following for a set</p> <pre><code>set&lt;int&gt; myset; set&lt;int&gt;::iterator it,itlow,itup; for (int i=1; i&lt;10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itup=myset.upper_bound (50); // cout &lt;&lt; "upper_bound at position " &lt;&lt; (*itup) &lt;&lt; endl; //output: 60 </code></pre> <p>How do i do this for a map? i think the program below seem to be using the first value of the map instead of the second value hence i am getting errors.</p> <p><strong>How do i set it to use the second value?</strong></p> <pre><code>map&lt;int,int&gt; myset; map&lt;int,int&gt;::iterator it,itlow,itup; for (int i=1; i&lt;10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 itup=myset.upper_bound (50); // cout &lt;&lt; "upper_bound at position " &lt;&lt; (*itup).second &lt;&lt; endl; //output: some random value returns </code></pre> <p>Actual Code that gives me wrong values when using with map, works when i use set : </p> <pre><code>int x = 50; map&lt;int,int&gt; myset; //for (int i=1; i&lt;10; i++) myset.insert(i*10); // 10 20 30 40 50 60 70 80 90 myset[0] = 10; myset[2] = 20; myset[3] = 30; myset[4] = 40; myset[5] = 50; myset[6] = 60; myset[7] = 70; map&lt;int,int&gt;::iterator begin,upbound,lobound,it; map&lt;int,int&gt;::reverse_iterator end; end = myset.rbegin(); begin = myset.begin(); upbound=myset.upper_bound(x); lobound=myset.lower_bound(x); lobound--; if(myset.size()==1) { cout &lt;&lt; "upper_range = " &lt;&lt; x &lt;&lt;endl; cout &lt;&lt; "lower_range = " &lt;&lt; x &lt;&lt;endl; } else if(x == (*begin).second) { cout &lt;&lt; "upper_range = " &lt;&lt; (*upbound).second &lt;&lt;endl; cout &lt;&lt; "lower_range = " &lt;&lt; end-&gt;second &lt;&lt;endl; } else if(x == end-&gt;second) { cout &lt;&lt; "upper_range = " &lt;&lt; (*begin).second &lt;&lt;endl; cout &lt;&lt; "lower_range = " &lt;&lt; (*lobound).second &lt;&lt;endl; } else { cout &lt;&lt; "start = " &lt;&lt; (*begin).second &lt;&lt;endl; cout &lt;&lt; "end = " &lt;&lt; end-&gt;second&lt;&lt;endl; cout &lt;&lt; "upper_range = " &lt;&lt; (*upbound).second &lt;&lt;endl; cout &lt;&lt; "lower_range = " &lt;&lt; (*lobound).second &lt;&lt;endl; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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