title: How can I use a struct as key in a std::map?
body: <p>I have the following code, but I get an error on on the last line:</p>
  <pre><code>struct coord { 
      int x, y; 
  
      bool operator=(const coord &amp;o) {
          return x == o.x &amp;&amp; y == o.y;
      }
  
      bool operator&lt;(const coord &amp;o) {
          return x &lt; o.x || (x == o.x &amp;&amp; y &lt; o.y);
      }
  };
  
  map&lt;coord, int&gt; m;
  pair&lt;coord, int&gt; p((coord{0,0}),123);
  m.insert(p); // ERROR here
  </code></pre>
  <p>How can I use a struct as key in a map?</p>
  <hr />
  <p>I tried to change the code to this:</p>
  <pre><code>struct coord { 
      int x, y; 
  
      bool const operator==(const coord &amp;o) {
          return x == o.x &amp;&amp; y == o.y;
      }
  
      bool const operator&lt;(const coord &amp;o) {
          return x &lt; o.x || (x == o.x &amp;&amp; y &lt; o.y);
      }
  };
  </code></pre>
  <p>But I'm still getting the following error:</p>
  <pre><code>C:\Users\tomc\Desktop\g&gt;mingw32-make g++ test.cpp -std=c++0x In file included from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/string:5 0:0,
                   from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/loc ale_classes.h:42,
                   from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/ios
  _base.h:43,
                   from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ios:43,
                   from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/ostream: 40,
                   from c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/iostream :40,
                   from test.cpp:1: c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function.h: In member  function 'bool std::less&lt;_Tp&gt;::operator()(const _Tp&amp;, const
  _Tp&amp;) const [with _ Tp = coord]': c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_tree.h:1184:4: inst antiated from 'std::pair&lt;std::_Rb_tree_iterator&lt;_Val&gt;, bool&gt; std::_Rb_tree&lt;_Key,  _Val, _KeyOfValue, _Compare,
  _Alloc&gt;::_M_insert_unique(const _Val&amp;) [with _Key
  = coord, _Val = std::pair&lt;const coord, int&gt;, _KeyOfValue = std::_Select1st&lt;std:: pair&lt;const coord, int&gt; &gt;, _Compare = std::less&lt;coord&gt;, _Alloc = std::allocator&lt;std::pair&lt;const coord, int&gt;&gt;]' c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_map.h:501:41: insta ntiated from 'std::pair&lt;typename std::_Rb_tree&lt;_Key, std::pair&lt;const _Key, _Tp&gt;,  std::_Select1st&lt;std::pair&lt;const _Key,
  _Tp&gt; &gt;, _Compare, typename _Alloc::rebind &lt;std::map&lt;_Key, _Tp,
  _Compare, _Alloc&gt;::value_type&gt;::other&gt;::iterator, bool&gt; std ::map&lt;_Key, _Tp, _Compare, _Alloc&gt;::insert(const std::map&lt;_Key, _Tp,
  _Compare, _ Alloc&gt;::value_type&amp;) [with _Key = coord, _Tp = int,
  _Compare = std::less&lt;coord&gt;,  _Alloc = std::allocator&lt;std::pair&lt;const coord, int&gt; &gt;, typename std::_Rb_tree&lt;_ Key, std::pair&lt;const _Key,
  _Tp&gt;, std::_Select1st&lt;std::pair&lt;const _Key, _Tp&gt; &gt;, _ Compare, typename _Alloc::rebind&lt;std::map&lt;_Key, _Tp, _Compare,
  _Alloc&gt;::value_ty pe&gt;::other&gt;::iterator = std::_Rb_tree_iterator&lt;std::pair&lt;const coord, int&gt; &gt;, st d::map&lt;_Key,
  _Tp, _Compare, _Alloc&gt;::value_type = std::pair&lt;const coord, int&gt;]' test.cpp:56:12:   instantiated from here c:\mingw\bin\../lib/gcc/mingw32/4.5.2/include/c++/bits/stl_function.h:230:22: er ror: passing 'const coord' as 'this' argument of 'const bool coord::operator&lt;(co nst coord&amp;)' discards qualifiers mingw32-make: *** [game] Error 1
  </code></pre>