gpt4 book ai didi

c++ - boost::multi_index 如何与成员函数一起工作?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:11:50 25 4
gpt4 key购买 nike

如果我有一个 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 after std::list, where this limitation does not happen. This seemingly odd behavior is imposed by the way multi_index_containers work; if elements were allowed to be mutated indiscriminately, we could introduce inconsistencies in the ordered indices of the multi_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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com