title: Sort Order in STL map and set
body: <p>How are the user defined objects sorted in map and set?
  As far as I know, map/set are Sorted Associative Containers: the elements being inserted are sorted based on the key that it holds.</p>
  
  <p>But map and set internally use <code>operator &gt;</code> to sort their elements.</p>
  
  <p>From the SGI site, I have the following examples:</p>
  
  <pre><code>struct ltstr
  {
      bool operator()(const char* s1, const char* s2) const
      {
          return strcmp(s1, s2) &lt; 0;
      }
  };
  
  int main()
  {
      map&lt;const char*, int, ltstr&gt; months;
  
      months["january"] = 31;
      months["february"] = 28;
      months["march"] = 31;
      months["april"] = 30;
      months["may"] = 31;
      months["june"] = 30;
      months["july"] = 31;
      months["august"] = 31;
      months["september"] = 30;
      months["october"] = 31;
      months["november"] = 30;
      months["december"] = 31;
  
      cout &lt;&lt; "june -&gt; " &lt;&lt; months["june"] &lt;&lt; endl;
  
      map&lt;const char*, int, ltstr&gt;::iterator cur  = months.find("june");
      map&lt;const char*, int, ltstr&gt;::iterator prev = cur;
      map&lt;const char*, int, ltstr&gt;::iterator next = cur;
  
      ++next;
      --prev;
  
      cout &lt;&lt; "Previous (in alphabetical order) is " &lt;&lt; (*prev).first &lt;&lt; endl;
      cout &lt;&lt; "Next (in alphabetical order) is " &lt;&lt; (*next).first &lt;&lt; endl;
  }
  </code></pre>
  
  <p>In the above example, how are the values sorted?</p>
  
  <p>Edit: Code moved from comment:</p>
  
  <pre><code>typedef map &lt;string, int&gt; Mint ;
  
  int main() 
  {
      string Name ;
      int Marks;
      Mint Grade;
      for (int i = 0; i&lt;4; i++) 
      {
          cin&gt;&gt; Name ; 
          cin &gt;&gt; Marks; 
          Grade [Name] = Marks ; 
      } 
      Mint :: iterator iter; 
      for( iter = Grade.begin(); iter != Grade.end(); iter++)
          cout&lt;&lt; (*iter).first&lt;&lt;“ \t ” &lt;&lt;(*iter).second&lt;&lt;“\n” ; 
      return 0; 
  
  } 
  </code></pre>
  
  <p>How would the values get sorted?</p>