I am tryig to use a custom hash function and a custom key with uordered\_map.
The problem is: when the key doesnt exist i need to copy the content of pointer (void \*record) to (void \*key.buffer) because the (void \* record) will be freed causing (void \*key.buffer) point to an invalid location.

The code is working correctly, but i am thinking if there is a better way (with better performance and elegante code) to do the same. I am inserting a new value inside a try-catch clause. :(

CASE1:

```
struct Key
{
    void *buffer;
    int size;
};

bool operator==(Key const &p1, Key const &p2)
{ 
    int ret = memcmp(p1.buffer, p2.buffer, p1.size);
    return (ret == 0);
}

//sorry, i forgot to put the hash_value
size_t hash_value(Key const& k)
{
//return (*(int *)(k.buffer));
return  MurmurHash2(k.buffer, k.size, 99);
}

BOOST_FIXTURE_TEST_CASE(  HashGroupBy_NoFilter, HashGroup_Init){

void *record = 0;
int actualBlock = 0;
typedef boost::unordered_map<Key, int>::iterator iter_type;

boost::unordered_map<Key,int> groupByMap;
Key valueKey;
ds->open();
while (ds->getNextBlock(actualBlock)){
    for (int i =0; i<ds->getRecordsInBlock(); i++){
        record = ds->getNextRecord(i);  
        valueKey.size = ds->dsMetadata->fieldSize;
        valueKey.buffer = record;
        try
        {
            int &count = groupByMap.at(valueKey);
            count ++;
        }
        catch (...)
        {
            valueKey.buffer = new char[valueKey.size];
            memcpy(valueKey.buffer, record, valueKey.size);

            std::pair<Key,int> recValue (valueKey, 1);
            groupByMap.insert(recValue);
        }

        matchRecords++;
    }
    actualBlock++;
}

```

}

If i use "count" i will pay a "hash time". In the case that the key exist i will have to pay another hash time to get the value. So i think it is worst than case 1.

CASE2: 

```
if (groupByMap.count(valueKey)){ //exist
  //pay hash calculation to get value
} else{
  //pay hash calculation to insert
}

```