title: How does std:map check whether two objects are equal?
body: <p>Why does the following code print <code>1</code>, even though I insert two elements into the map?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;map&gt;
  #include &lt;string&gt;
  #include &lt;utility&gt;
  
  struct Foo
  {
    Foo(int bar, const std::string&amp; baz)
      : bar(bar)
      , baz(baz)
    {}
  
    int bar;
    std::string baz;
  
    bool operator&lt;(const Foo&amp; rhs) const
    {
      if (bar &lt; rhs.bar &amp;&amp; baz &lt; rhs.baz)
      {
        return true;
      }
      else
      {
        return false;
      }
    }
  };
  
  int main()
  {
      Foo first(0, "test");
      Foo second(1, "test");
      std::map&lt;Foo, std::string&gt; m;
      m.insert(std::make_pair(first, "test"));
      m.insert(std::make_pair(second, "test1"));
      std::cout &lt;&lt; m.size() &lt;&lt; std::endl;
  }
  </code></pre>
  
  <p>The second call to <code>insert()</code> says that we already have that item in the map. Why?</p>
  
  <p>My <a href="/q/39979773">previous question</a> was mistakenly closed because of the typo. I know that <code>insert</code> will tell you whether that item is already in the container.</p>