title: Use user defined struct as the key for map C++
body: <p>I am trying to map my own class to an integer:</p>
  
  <pre><code>struct point{
    int x;
    int y;
    bool operator==(const point&amp; b) {
      return (x==b.x &amp;&amp; y==b.y);
    };
    bool operator&lt;(const point&amp; b) {
      return (x &lt; b.x &amp;&amp; y &lt; b.y);
    };
  };
  
  int main() {
    point a, b;
    a.x = 0;
    a.y = 1;
    b.x = 0;
    b.y = 1;
    map&lt;point, int&gt; myMap;
    myMap[a] = 1;
    cout &lt;&lt; (a==b) &lt;&lt; endl;
    cout &lt;&lt; myMap.count(b) &lt;&lt; endl;
  }
  </code></pre>
  
  <p>This code does not compile and I cannot understand the error message. It seems that if I change into <code>map&lt;point*, int&gt;</code> it will compile but it is not what I want. My purpose is that when I call <code>mayMap.count(b)</code>, it will return 1. What changes shall I make? 
  Thank you very much!
  Error Message:</p>
  
  <pre><code>-*- mode: compilation; default-directory: "~/Dropbox/Documents/UVa/Others/UVa Online Judge/" -*-
  Compilation started at Tue Nov 12 14:04:15
  
  clang++ testset.cpp
  In file included from testset.cpp:1:
  In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iostream:38:
  In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/ios:216:
  In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__locale:15:
  In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/string:434:
  In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/algorithm:594:
  In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/memory:597:
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/__functional_base:56:21: error: 
        invalid operands to binary expression ('const point' and 'const point')
          {return __x &lt; __y;}
                  ~~~ ^ ~~~
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/map:1032:17: note: 
        in instantiation of member function 'std::__1::less&lt;point&gt;::operator()'
        requested here
              if (__tree_.value_comp().key_comp()(__k, __nd-&gt;__value_.first))
                  ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/map:1272:36: note: 
        in instantiation of member function 'std::__1::map&lt;point, int,
        std::__1::less&lt;point&gt;, std::__1::allocator&lt;std::__1::pair&lt;const point,
        int&gt; &gt; &gt;::__find_equal_key' requested here
      __node_base_pointer&amp; __child = __find_equal_key(__parent, __k);
                                     ^
  testset.cpp:41:12: note: in instantiation of member function
        'std::__1::map&lt;point, int, std::__1::less&lt;point&gt;,
        std::__1::allocator&lt;std::__1::pair&lt;const point, int&gt; &gt; &gt;::operator[]'
        requested here
    secondMap[a] = 1;
             ^
  testset.cpp:13:8: note: candidate function not viable: 'this' argument has type
        'const point', but method is not marked const
    bool operator&lt;(const point&amp; b) {
         ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/utility:375:1: note: 
        candidate template ignored: failed template argument deduction
  operator&lt; (const pair&lt;_T1,_T2&gt;&amp; __x, const pair&lt;_T1,_T2&gt;&amp; __y)
  ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:565:1: note: 
        candidate template ignored: failed template argument deduction
  operator&lt;(const reverse_iterator&lt;_Iter1&gt;&amp; __x, const reverse_iterator&lt;_I...
  ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:960:1: note: 
        candidate template ignored: failed template argument deduction
  operator&lt;(const move_iterator&lt;_Iter1&gt;&amp; __x, const move_iterator&lt;_Iter2&gt;&amp; __y)
  ^
  /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/c++/v1/iterator:1269:1: note: 
        candidate template ignored: failed template argument deduction
  operator&lt;(const __wrap_iter&lt;_Iter1&gt;&amp; __x, const __wrap_iter&lt;_Iter2&gt;&amp; __y...
  ^
  1 error generated.
  
  Compilation exited abnormally with code 1 at Tue Nov 12 14:04:16
  </code></pre>