title: Using comparator for STL set
body: <p>Check the following code:</p>
  
  <pre><code>string toLowerCase(const string&amp; str) {
      string res(str);
      int i;
  
      for (i = 0; i &lt; (int) res.size(); i++)
          res[i] = (char) tolower(res[i]);
  
      return res;
  }
  
  class LeagueComparator
  {
  public:
      bool operator()(const string&amp; s1, const string&amp; s2)
      {
          return toLowerCase(s1) &lt; toLowerCase(s2);
      }
  };
  
  int main()
  {
      set&lt;string, LeagueComparator&gt; leagues;
      set&lt;string, LeagueComparator&gt;::iterator iter;
  
      leagues.insert("BLeague");
      leagues.insert("aLeague");    // leagues = {"aLeague", "BLeague"}
      leagues.insert("ALeague");
  
      for (iter = leagues.begin(); iter != leagues.end(); iter++)
          cout &lt;&lt; *iter &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>aLeague
  BLeague
  </code></pre>
  
  <p>which is shocking to me. I thought (and expecting) the output would be:</p>
  
  <pre><code>aLeague
  ALeague
  BLeague
  </code></pre>
  
  <p>Before the execution of <code>leagues.insert("ALeague");</code>, the <code>leagues</code> contains <code>"aLeague"</code> and <code>"BLeague"</code>. My question is, while executing <code>leagues.insert("ALeague");</code> why the machine treats <code>"ALeague" == "aleague"</code>? According to my understanding, there is no element <code>"ALeague"</code> in <code>leagues</code>. So <code>"ALeague"</code> should be inserted into <code>leagues</code>. The comparator should determine where to put <code>"ALeague"</code>.</p>
  
  <p>Thanks in advance.</p>
  
  <p>PS: Please don't hit me for using C style cast. :P I'm too lazy to type <code>static_cast</code>.</p>