开发者

hash function for boost_unordered, no default possible?

I have an class which has list of items (an instance of a class called ItemList). This ItemList class has functions such as:

ItemIndexType AddItem(...);
void DoSomething(ItemIndexType index, ...);

ItemIndexType acts as some kind of "smart iterator", and has the member variables

myIter (iterator to a multimap in the ItemList),

myList (pointer to the ItemList).

And a few more used for house-keeping.

It is smart since, if I would erase an item from the ItemList, all indices which point to the item will get a cleared. (pointing to the end of the map).

It is working as intended right now, an开发者_如何学God I rather not change it too much anymore. However for the application I need to do something extra: I'm building a "reset" function:

void ResetItem(ItemIndexType ind);

This function would then "reset" the item ind points to to the original value. I don't want to store the orignal value within the item class, so I decided to create a container which would have as key the itemindex and as value the original arguments with which the item was constructured.

Since ordering is not only not needed, it is also impossible, I think boost::unordered is the way to go.

Changing the deque I used to keep track of the indices to the hash table yielded a weird error though:

X\boost\functional\hash\extensions.hpp(176) : error C2665: 'boost::hash_value' : none of the 16 overloads could convert all the argument types

Where the list is created by:

typedef boost::unordered_map<ItemIndexType, ListDataType> ListContType;

How to create a hash-function for a custom datatype?


You need an equality predicate and a hash function for your key datatype, see Equality Predicates and Hash Functions. You can build your hash function by combining the hash functions of simple types with boost::hash_combine

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜