小编Jua*_*rez的帖子

修改具有 hashed_non_unique 键的 Boost 多索引映射中的键范围

我正在尝试更新多索引映射中的一系列元素,但它似乎比我预期的要复杂一些。

鉴于以下声明:

struct Information {

    int number() const {
        return number_;
    }

    int number_;
};


typedef boost::multi_index_container<
        Information, 
        boost::multi_index::indexed_by<
                boost::multi_index::hashed_non_unique<
                        boost::multi_index::tag<int>,
                        boost::multi_index::const_mem_fun<
                                Information, 
                                int, 
                                &Information::number
                        >
                >
        >
    > Information_Map;

Information_Map information_map_;
Run Code Online (Sandbox Code Playgroud)

以下是对上述声明内容的总结:

  • 一个 struct Information,它包含一个任意数字,它不是唯一的
  • 具有以下属性的多索引映射
    • 包含类型元素 Information
    • 元素使用非唯一散列进行索引
    • 散列的键是使用成员函数构造的 Information::number()

现在,我也宣布了这样的事情:

struct Plus_One_Modifier {
    void operator()(Information& obj) const {
        obj.number_ += 1;
    }
};
Run Code Online (Sandbox Code Playgroud)

前面的struct是一个修饰符,预计在调用modify函数时会用到:

// from boost/multi_index/hashed_index.hpp
template<typename Modifier> bool modify(iterator position,Modifier mod);
Run Code Online (Sandbox Code Playgroud)

当此函数仅用于修改一个元素时,一切都按预期工作:

// Updates the first element
Information_Map::index<int> index = information_map_.get<int>();
index.modify(index.begin(), Plus_One_Modifier());
Run Code Online (Sandbox Code Playgroud)

该 …

c++ hash boost multidimensional-array

3
推荐指数
1
解决办法
830
查看次数

标签 统计

boost ×1

c++ ×1

hash ×1

multidimensional-array ×1