title: How to have a set of structs in C++
body: <p>I have a struct which has a unique key. I want to insert instances of these structs into a set. I know that to do this the &lt; operator has to be overloaded so that set can make a comparison in order to do the insertion.</p>
  
  <p>The following does not work:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;set&gt;
  using namespace std;
  struct foo 
  {
        int key;
  };
  
  bool operator&lt;(const foo&amp; lhs, const foo&amp; rhs)
  {
        return lhs.key &lt; rhs.key;
  }
  
  set&lt;foo&gt; bar;
  
  int main()
  {
      foo *test = new foo;
      test-&gt;key = 0;
      bar.insert(test);
  }
  </code></pre>