Closest element in std map when discarding values I have the following situation: I have two std::map&lt;int,float&gt; and I want to go through the first map one by one and find the element in the second map with the key closest to the key in the first map. Now, this plain situation is obviously addressed in other question like e.g. this one, but here is where my situation deviates: 
There might be elements in the second map that I do not want to consider because the value is below some criterion and thus even if the element is the closest, it will be discarded and now I have to check two other elements against the key. Below is a bit of dummy code that illustrates the input data and also a bit of code that seems to achieve what I want, but I am not sure if there is maybe a better/cleaner way of doing this. 

#include &lt;iostream&gt;
#include &lt;map&gt;
#include &lt;cmath&gt;

void setClosest( std::map&lt;int,float&gt;::iterator &amp; it, std::map&lt;int,float&gt;::iterator &amp;upper, std::map&lt;int,float&gt;::iterator &amp;lower, int key ) {
  it = fabs( upper-&gt;first - key ) &lt; fabs( lower-&gt;first - key ) ? upper : lower;
}


int main(int, char**)
{
  std::map&lt;int, float&gt; map1;
  std::map&lt;int, float&gt; map2;

  map1[5000]  = 5.5;
  map1[10000] = 5.2;
  map1[12000] = 5.7;
  map1[15000] = 5.3;
  map1[20000] = 5.5;
  map1[25000] = 5.9;

  map2[10400] = 523.0;
  map2[12500] = 99.0;
  map2[15200] = 67.0;
  map2[15400] = 76.0;
  map2[20200] = 511.0;
  map2[25800] = 567.0;

  std::map&lt;int,float&gt;::iterator it = map1.begin();
  std::map&lt;int,float&gt;::iterator lower;
  std::map&lt;int,float&gt;::iterator upper;
  std::map&lt;int,float&gt;::iterator closest;

  for( ; it != map1.end(); ++it ) {
    int key     = it-&gt;first;
    float value = it-&gt;second;

    lower = map2.lower_bound(key);
    upper = ( lower == map2.begin() ? lower : lower-- );

    setClosest( closest, upper, lower, key);

    std::cout &lt;&lt; key &lt;&lt; ' ' &lt;&lt; upper-&gt;first &lt;&lt; ' ' &lt;&lt; lower-&gt;first &lt;&lt; " -&gt; " &lt;&lt; closest-&gt;first &lt;&lt; std::endl;

    while( closest-&gt;second &lt; 100 ) {
      std::cout &lt;&lt; "skipping too low value " &lt;&lt; closest-&gt;first &lt;&lt; ' ' &lt;&lt; closest-&gt;second &lt;&lt; std::endl;
      closest == upper ? upper++ : lower--;
      setClosest( closest, upper, lower, key);
    }

    std::cout &lt;&lt; key &lt;&lt; ' ' &lt;&lt; upper-&gt;first &lt;&lt; ' ' &lt;&lt; lower-&gt;first &lt;&lt; " -&gt; " &lt;&lt; closest-&gt;first &lt;&lt; std::endl;

  }

  return 0;
}


Output

5000 10400 10400 -&gt; 10400
5000 10400 10400 -&gt; 10400
10000 10400 10400 -&gt; 10400
10000 10400 10400 -&gt; 10400
12000 12500 10400 -&gt; 12500
skipping too low value 12500 99
12000 15200 10400 -&gt; 10400
15000 15200 12500 -&gt; 15200
skipping too low value 15200 67
skipping too low value 15400 76
skipping too low value 12500 99
15000 20200 10400 -&gt; 10400
20000 20200 15400 -&gt; 20200
20000 20200 15400 -&gt; 20200
25000 25800 20200 -&gt; 25800
25000 25800 20200 -&gt; 25800
Consider replacing the while loop with two for loops that increment lower and upper iterators till they have a suitable value - and only afterwards setting closest - it might be doing more operations but it is more cache friendly.

Or simply create a new map2 with only valid values (simply filtrate unwanted). 

Though, different situation have different preferred solutions - there is a trade-off which solution is better depending on number of undesirable values in map2 and relative sizes of map1 and map2.

I don't think that you can improve it beyond that, lest you have some extra knowledge you haven't shared. In certain cases, you might want to implement some completely different solutions that are not reliant on map.

About the code itself, there are just a few minor issues:


You don't take care of the case when lower_bound return an end, in this case program will crash or UB. Same problem inside the while scope.
There is no need to declare the four iterators outside the scope. Just declare them with auto when you instantiate them. Nobody cares to see std::map&lt;int,float&gt;::iterator.
replace setClosest with

  template&lt;typename MapItr&gt;
  auto getClosestMapIterator(const MapItr &amp;upper, const MapItr &amp; lower, int key ) -&gt; const MapItr&amp; 
  {
     return abs( upper-&gt;first - key ) &lt; abs( lower-&gt;first - key ) ? upper : lower;
  }


and adjust usage auto closest = getClosestMapIterator(upper, lower, key); Also you better use abs instead of fabs, as fabs is for floating point types, so you unnecessarily cast integers to double with fabs.
Inside while the line closest == upper ? upper++ : lower--; is confusing. Simply write if scope. Don't use the expression cond ? ret_on_true : ret_on_false for side-effects, only when you want to get the return value.