
Question:
as I've just learned in <a href="https://stackoverflow.com/questions/1699704/boostmultiindexcontainer-with-randomaccess-and-orderedunique" rel="nofollow">in my other question</a>, I could use a composite_key
for a struct, which has a std::vector
and an integer. Now my question is: Can I use this somehow to work with hashed_indecies?
Here an example similar to <a href="http://www.boost.org/doc/libs/1_40_0/libs/multi_index/doc/tutorial/key_extraction.html#composite_keys_hash" rel="nofollow">THIS</a>:
struct unique_property
{
//the pair of int and std::vector<int> shall be unique
int my_int;
std::vector<int> my_vec;
};
typedef multi_index_container<
unique_property,
indexed_by<
hashed_unique< // indexed by my_int and every entry of my_vec
composite_key<
street_entry,
member<unique_property,int,&unique_property::my_int>,
member<unique_property,std::vector<int>,&unique_property::my_vec>
>
>,
random_access< >
>
> property_locator;
The problem is (of course) that a std::vector<int>
is no suitable hash-key. Can I put this code in an elegant wrapper (or something like that), to produce a hash-key from every entry of my_vec
as well?
Use code snippet from your suggestion <a href="https://stackoverflow.com/questions/1699704/boostmultiindexcontainer-with-randomaccess-and-orderedunique/1700899#1700899" rel="nofollow">here</a>. It should work. I've added my comments there.
Answer2:If you want vector to be hashable, you can just write a hash<vector<int> >
function in namespace std
, making it hash however you please (hint: you might be able to get away with hashing only the first few elements in some applications). This should make std::hash_set<vector<int> >
work, and I think your more complex container as well.