Boost Multi-Index:vector和int的复合键,用于散列索引

时间:2009-11-09 13:34:56

标签: c++ boost vector multi-index

正如我刚刚在in my other question中学到的,我可以使用composite_key作为结构,其中包含std::vector和整数。现在我的问题是:我能以某种方式使用它来使用hashed_indecies吗?

此处的示例类似于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;

问题是(当然)std::vector<int>不是合适的散列键。我可以将此代码放在一个优雅的包装器(或类似的东西)中,以便从my_vec的每个条目生成一个哈希键吗?

2 个答案:

答案 0 :(得分:0)

使用您的建议here中的代码段。它应该工作。我在那里添加了我的评论。

答案 1 :(得分:0)

如果你想要矢量是可以清除的,你可以在hash<vector<int> >中编写一个namespace std函数,然后根据你的需要进行哈希处理(提示:你可能只能在第一次哈希中使用哈希)一些应用中的一些元素)。这应该使std::hash_set<vector<int> >起作用,我认为你的容器也更复杂。