我正在尝试更新多索引映射中的一系列元素,但它似乎比我预期的要复杂一些。
鉴于以下声明:
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)
以下是对上述声明内容的总结:
Information,它包含一个任意数字,它不是唯一的InformationInformation::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)
该 …