如果我有一个 boost::multi_index 如下,
typedef multi_index_container<
employee,
indexed_by<
hashed_unique<mem_fun<employee, std::string, &employee::getname> >,
hashed_unique<mem_fun<employee, int, &employee::getage> >
>
> employee_set;
我知道插入到这个容器中的“employees”类对象的存储方式可以在 O(1) 时间内检索到(作为 HashMap )。
当成员变量(姓名、年龄)在程序执行过程中更新(比如可能使用 setname 或 setage 之类的东西)并且仍然使用这些值进行散列时,它将如何存储?我理解错了吗?
TIA
-R
最佳答案
来自documentation :
The iterators provided by every index are constant, that is, the elements they point to cannot be mutated directly. This follows the interface of
std::set
for ordered indices but might come as a surprise for other types such as sequenced indices, which are modeled afterstd::list
, where this limitation does not happen. This seemingly odd behavior is imposed by the waymulti_index_container
s work; if elements were allowed to be mutated indiscriminately, we could introduce inconsistencies in the ordered indices of themulti_index_container
without the container being notified about it. Element modification is properly done by means of update operations on any index.
换句话说,你只有 const
访问你存储的对象,除非你使用容器的更新功能,此时它可以 Hook 调用并即时调整哈希值.
关于c++ - boost::multi_index 如何与成员函数一起工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40108480/