title: How to find the difference of two maps?
body: <p>I'm trying to implement a similar function to set_intersection and set_difference but instead using maps as my date type for an assignment. I'm able to find the intersection, but my output for the difference function is not what I should expecet and I cannot figure out why. The two functions I'm using are as follows. </p>
  
  <pre><code>map&lt;string, int&gt; Group::Intersection(map&lt;string, int&gt; &amp;tempSet, map&lt;string, int&gt; &amp;currentSet){
      map&lt;string, int&gt;::const_iterator input_iterator;
      map&lt;string, int&gt; result;
      typename map&lt;string, int&gt;::const_iterator it1 = tempSet.begin();
      typename map&lt;string, int&gt;::const_iterator it2 = currentSet.begin();
  
      while (it1 != tempSet.end() &amp;&amp; it2 != currentSet.end())
      {
          if (it1-&gt;first &lt; it2-&gt;first)
              ++it1;
          else if (it2-&gt;first &lt; it1-&gt;first)
              ++it2;
          else{
              result.insert(make_pair(it2-&gt;first, min(it1-&gt;second, it2-&gt;second)));
              ++it1;
              ++it2;
          }
      }
  
      return result;
  }
  
  map&lt;string, int&gt; Group::Difference(map&lt;string, int&gt; &amp;tempSet, map&lt;string, int&gt; &amp;currentSet){
      map&lt;string, int&gt;::const_iterator input_iterator;
      map&lt;string, int&gt; result;
      typename map&lt;string, int&gt;::const_iterator it1 = tempSet.begin();
      typename map&lt;string, int&gt;::const_iterator it2 = currentSet.begin();
  
      while (it1 != tempSet.end() &amp;&amp; it2 != currentSet.end())
      {
          if (it1-&gt;first &lt; it2-&gt;first){
              result.insert(make_pair(it1-&gt;first, abs(it1-&gt;second - it2-&gt;second)));
              ++it1;
          }
          else if (it2-&gt;first &lt; it1-&gt;first){
              result.insert(make_pair(it2-&gt;first, abs(it1-&gt;second - it2-&gt;second)));
              ++it2;
          }
          else{            
              ++it1;
              ++it2;
          }
      }
      return result;
  }
  </code></pre>
  
  <p>Using these two lists: </p>
  
  <pre><code>cat 5
  dog 4
  bear 2
  dinosaur 1
  
  cat 6
  dog 4
  snake 3
  lion 4
  </code></pre>
  
  <p>I receive an intersection of cat 5 and dog 4, but the difference gives me bear 4 and dinosaur 3. As far as I can tell, this is taking the two keys from the first map and matching them to the values from the second map but I cannot determine if it's my iteration or when I am putting the keys/values into a new map. Any advice would be greatly appreciated because I feel this should be a simple solution which I cannot figure out.</p>