title: Removing entries of particular key in C++ STL multimap
body: <p>I have this sample code to insert entries to a multimap. I am trying to delete particular entries of a specified key. But this code goes into infinite loop. Can someone help me with this code?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;string&gt;
  using namespace std;
  
  int main()
  {
      multimap&lt;string, string&gt; names;
      string n;
  
      names.insert(pair&lt;string, string&gt;("Z", "F"));
      names.insert(pair&lt;string, string&gt;("Z", "A"));
  
      names.insert(pair&lt;string, string&gt;("S", "T"));
      names.insert(pair&lt;string, string&gt;("S", "A"));
      names.insert(pair&lt;string, string&gt;("S", "J"));
  
      names.insert(pair&lt;string, string&gt;("D", "H"));
      names.insert(pair&lt;string, string&gt;("D", "W"));
      names.insert(pair&lt;string, string&gt;("D", "R"));
  
      multimap&lt;string, string&gt;::iterator p;
  
  
      p = names.find("Z");
      if(p != names.end()) { // found a name
          do {
              cout &lt;&lt; n &lt;&lt; ", " &lt;&lt; p-&gt;second;
              cout &lt;&lt; endl;
              if (p-&gt;second.compare("A") == 0) {
                  names.erase(p);
                  p++;
              } else {
                  p++;
              }
          } while (p != names.upper_bound("Z"));
      }
      else{
          cout &lt;&lt; "Name not found.\n";
      }
  
      p = names.find("Z");
      if(p != names.end()) { // found a name
          do {
              cout &lt;&lt; n &lt;&lt; ", " &lt;&lt; p-&gt;second;
              cout &lt;&lt; endl;
          } while (p != names.upper_bound("Z"));
      }
      else{
          cout &lt;&lt; "Name not found.\n";
      }
      return 0;
  }
  </code></pre>
  
  <p>In the above I am looking up using Key value "Z" and want to delete "A".</p>