Struct as unordered_map key

I have a problem with putting data in unordered_map using struct as a key:


typedef struct osdpi_flow_identificator {
 u32 lower_ip;
 u32 upper_ip;
 u16 lower_port;
 u16 upper_port;
 u8 protocol;
} osdpi_flow_identificator_t;

// custom hash function for osdpi_flow_identificator
struct osdpi_flow_hash {
 std::size_t operator() (osdpi_flow_identificator * key) const {
  hash hash_function;
  std::size_t returnValue =
   hash_function(key->lower_ip)
   + hash_function(key->upper_ip)
   + hash_function(key->lower_port)
   + hash_function(key->upper_port)
   + hash_function(key->protocol);

  printf(" calculated hash: %i\n", returnValue);

  return returnValue;
 }
};

typedef unordered_map <osdpi_flow_identificator*, osdpi_flow*, osdpi_flow_hash> osdpi_flows_hashmap;
typedef osdpi_flows_hashmap::value_type osdpi_flows_pair;
static osdpi_flows_hashmap osdpi_flows;

My problem is that the hash function returns the same value for osdpi_flow_identificators with the same value, but


 osdpi_flow_identificator * flow_id = new osdpi_flow_identificator;
 osdpi_flows_hashmap::const_iterator iter;
 iter = osdpi_flows.find(flow_id);
 if (iter != osdpi_flows.end()) {
  ...

does not find it, although a record with flow_id that has exactly the same values ​​is already in the hash map. I checked it, outputting the entire hash map, and also the hash function returns the same value. Therefore, it is very difficult for me to understand why unordered_map cannot find an entry with the same hash.

I also tried to overload operator == and operator <, which I sometimes found as a tip on the network, but these functions were not called either.

, , , , , flow_id - .

!

+5
1

, : -)

EqualOperator , . - :

struct Eq
{
  bool operator() ( osdpi_flow_identificator * id1, osdpi_flow_identificator * id2) const
  {
    id1->lower_ip == id2->lower_ip && // compare all fields.
  }
};

-

typedef unordered_map <osdpi_flow_identificator*, osdpi_flow*, osdpi_flow_hash, Eq> osdpi_flows_hashmap;

- hashmap . , .

+1

All Articles