Boost Multi-Index : Composite key of vector and in

2019-08-27 21:33发布

问题:

as I've just learned in in my other question, 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 THIS:

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?

回答1:

Use code snippet from your suggestion here. It should work. I've added my comments there.



回答2:

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.