Boost Multi-Index : Composite key of vector and int for hashed indices

拟墨画扇 提交于 2019-12-11 14:18:23

问题


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.



来源:https://stackoverflow.com/questions/1701022/boost-multi-index-composite-key-of-vector-and-int-for-hashed-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!