I have struct that store info about ARP packet and I need to save the ARP request and reply packet together. I would like to use std::map for this, the key and mapped value is that ARP struct, and I would be using the find operator to math the reply with request.
My question is how can I say map::find to compare only some members from struct.
Example of what I’m trying to describe
struct arp_packet : public packet_info
{
u_short type; // 0x0001 request
// 0x0002 reply
struct ipv4_address ip_sender;
struct ipv4_address ip_target;
};
I would save all request in map like this
std::map<arp_packet*, arp_packet*>
First the mapped value is NULL but whet the reply packet comes I will use the find method to match it to request.
So how should I accomplist that the map would take as key the arp_packet *, and in find it would take the another arp_packet * and match it using ip_sender and ip_target?
A
std::mapaccepts up to 4 template parameters (the last 2 have default values).You would be using the 3rd parameter here: a functor meant to compare two keys.
Be careful about implementing a proper
operator()semantics, it should match the mathematical properties of<, notably antisymmetry and transitivity.