title: How can I print values of a key in sorted order in a multimap
body: <p>I need to design a data structure that basically stores key-value pairs with key being an integer and its value a string.</p>
  
  <p>Condition 1: There could be multiple values associated with a Key.</p>
  
  <p>Condition 2: I need to print all the keys stored in this map in descending order.</p>
  
  <p>Condition 3: Though the Keys(integers) are printed in Descending order, their corresponding values(strings) must be printed in lexicographic(sorted in ascending) order.</p>
  
  <p><strong>Sample Input:</strong>                               </p>
  
  <pre><code>78 Eve                                      
  99 Bob                                       
  78 Alice                                    
  </code></pre>
  
  <p><strong>Expected Output:</strong></p>
  
  <pre><code>99 Bob
  78 Alice
  78 Eve
  </code></pre>
  
  <p>Notice the keys are in descending order, while the values are in ascending order.</p>
  
  <p>To do this I have come up with below code in C++:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  
  using namespace std;
  
  int main()
  {
      int N;
      string name;
      int marks;
      multimap&lt;int, string, greater&lt;int&gt;&gt; studMap;
      multimap&lt;int, string, greater&lt;int&gt;&gt;::iterator itBeg, itEnd;
      typedef multimap&lt;int, string, greater&lt;int&gt;&gt;::iterator mapIter;
  
      cin &gt;&gt; N;   // total no. of key-value pairs input by user
  
      while (N--)
      {
          cin &gt;&gt; name &gt;&gt; marks;  // pairs of value-key input by user - N times
          studMap.insert(pair&lt;int, string&gt;(marks, name));
      }
  
      for (itBeg = studMap.begin(); itBeg != studMap.end(); itBeg = itEnd)
      {
          marks = itBeg-&gt;first;
  
          pair&lt;mapIter, mapIter&gt; keyRange = studMap.equal_range(marks);
  
          for (itEnd = keyRange.first; itEnd != keyRange.second; ++itEnd)
          {
              cout &lt;&lt; marks &lt;&lt; " " &lt;&lt; itEnd-&gt;second &lt;&lt; endl;
          }
      }  
      return 0;
  }
  </code></pre>
  
  <p>But I am getting output as shown below:</p>
  
  <pre><code>99 Bob
  78 Eve 
  78 Alice
  </code></pre>
  
  <p>whereas I need pair(78, Alice) to be printed before (78, Eve)</p>