title: C++ Map inserts pairs with duplicated key
body: <p>I'm trying to use map with MAC as key. I wrote user defined class for MAC and overload operator&lt; to use in map.</p>
  
  <pre><code>class Mac {
  public:
      uint8_t addr[6];
  
      std::string to_string() const {
          char mac_to_string[18];
  
          sprintf(mac_to_string, "%02X:%02X:%02X:%02X:%02X:%02X", addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
  
          return std::string(mac_to_string);
      }
  
      Mac &amp;operator=(const Mac &amp;rhs) {
          memcpy(addr, rhs.addr, sizeof(Mac));
  
          return *this;
      }
  
      Mac &amp;operator=(uint8_t rhs[6]) {
          memcpy(addr, rhs, sizeof(Mac));
  
          return *this;
      }
  
      bool operator&lt;(const Mac &amp;rhs) const {
          return memcmp(addr, rhs.addr, sizeof(Mac)) != 0;
      }
  };
  </code></pre>
  
  <p>I have some logic that add data into map. Is true that insert function is called even MAC address is already in map. But map has to not insert if key exists but it isn't. The pair with same key has same data. How can I solve it?</p>
  
  <pre><code>class MacPair {
  public:
      Mac src_mac;
      Mac dst_mac;
  
      MacPair() {
  
      }
  
      MacPair(uint8_t src_mac[6], uint8_t dst_mac[6]) {
          this-&gt;src_mac = src_mac;
          this-&gt;dst_mac = dst_mac;
      }
  
      MacPair &amp;operator=(const MacPair &amp;rhs) {
          src_mac = rhs.src_mac;
          dst_mac = rhs.dst_mac;
  
          return *this;
      }
  
      bool operator&lt;(const MacPair &amp;rhs) const {
          return src_mac &lt; rhs.src_mac || dst_mac &lt; rhs.dst_mac;
      }
  };
  
  ...
  if (obj-&gt;mac_conversations.find(mac_pair) != obj-&gt;mac_conversations.end()) {
      mac_exists = true;
  }
  else if (obj-&gt;mac_conversations.find(rev_mac_pair) != obj-&gt;mac_conversations.end()) {
      mac_exists = true;
      mac_is_reverse = true;
  }
  
  if (!mac_exists) {
      obj-&gt;mac_conversations.insert(make_pair(mac_pair, PacketInfo()));
      obj-&gt;mac_endpoints.insert(make_pair(mac_pair.src_mac, PacketInfo()));
      obj-&gt;mac_endpoints.insert(make_pair(mac_pair.dst_mac, PacketInfo()));
      mac_exists = true;
  }
  ...
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/TzEX7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TzEX7.png" alt="enter image description here"></a></p>