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

标签 c++ c++11 boost multi-index boost-multi-index

如果我有一个 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/

相关文章:

C++ ifstream 不会打开任何文件

c++ - 如何 self 复制一个 vector ?

c++ - 在 [0, n) 中生成随机数

c++ - 创建一个 const shared_ptr<pure virtual class> 成员

c++ - Solaris Imakefile 复制文件规则

c++ - 为什么我的注册表读取程序会失败?

c++ - is_lock_free 未在 gcc 4.7.2 的 std::atomic<T> 中定义?

c++ - lambda 函数对象中的静态变量如何工作?

c++ - 使用 boost 序列化为二进制存档。输入流错误

c++ - Boost::网络信号封装