boost :: multi_index如何与成员函数一起使用?

时间:2022-09-09 07:27:39

If I have a boost::multi_index as follows,

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

I understand that the objects of class "employees" that are inserted into this container are stored in such a way that it can be retrieved in O(1) time(as a hash map).

我知道插入到这个容器中的类“employees”的对象以这样的方式存储,即它可以在O(1)时间内被检索(作为哈希映射)。

How will it be stored when the member variables(name, age) are updated during the course of the program(like maybe with something like setname or setage) and still be hashed using those values? Am I understanding something wrong?

如果在程序过程中更新成员变量(名称,年龄)(例如可能使用setname或setage)并且仍然使用这些值进行哈希处理,它将如何存储?我理解错了吗?

TIA

TIA

-R

-R

1 个解决方案

#1


1  

From the 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.

每个索引提供的迭代器都是常量,也就是说,它们指向的元素不能直接变异。这遵循std :: set为有序索引的接口,但对于其他类型(例如在std :: list之后建模的顺序索引)可能会出乎意料,其中不会发生此限制。这种看似奇怪的行为是由multi_index_containers的工作方式强加的;如果允许元素不加选择地变异,我们可能会在multi_index_container的有序索引中引入不一致,而不会通知容器。通过对任何索引的更新操作来正确地完成元素修改。

In other words, you only have const access to your stored objects, unless you use the container's updating functions, at which point it can hook into the call and adjust the hashes on-the-fly.

换句话说,除非您使用容器的更新功能,否则您只能访问存储的对象,此时它可以挂接到调用并动态调整哈希值。

#1


1  

From the 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.

每个索引提供的迭代器都是常量,也就是说,它们指向的元素不能直接变异。这遵循std :: set为有序索引的接口,但对于其他类型(例如在std :: list之后建模的顺序索引)可能会出乎意料,其中不会发生此限制。这种看似奇怪的行为是由multi_index_containers的工作方式强加的;如果允许元素不加选择地变异,我们可能会在multi_index_container的有序索引中引入不一致,而不会通知容器。通过对任何索引的更新操作来正确地完成元素修改。

In other words, you only have const access to your stored objects, unless you use the container's updating functions, at which point it can hook into the call and adjust the hashes on-the-fly.

换句话说,除非您使用容器的更新功能,否则您只能访问存储的对象,此时它可以挂接到调用并动态调整哈希值。