1

我正在使用 boost 1.48.0,我没有升级选项。

我已经构建了一个 multi_index_container,其中的索引具有重叠标准。共享相同索引标准的索引是如何产生影响的,如下例所示?示例代码的最后一行暗示了我的要求。

struct street_address_key : composite_key<
    t_postal_address
    , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::street_name>>
    , const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::house_number>>
    , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::city>>
    , const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::state>>
    , const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::zip_code>>
> {};

multi_index<
    t_postal_address
    indexed_by<
        ordered_unique<street_address_key>
        , ordered_non_unique<const_mem_fun<t_postal_address, string, &t_postal_address::name>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::street_name>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::house_number>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::city>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::state>>
        , ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, long, &t_postal_address::zip_code>>
    >
> t_address_book;

...

t_address_book::nth_element<0>::type::iterator address_iter = book.find(some_address_tuple);

book.modify(address_iter, modify_city_method);

auto city_range = book.get<4>().equal_range( \*???*\ );

城市索引与 street_address_key 共享相同的标准。据我了解,通过 street_address_key 进行修改将确保索引得到适当更新。但“城市”指数的状态如何?是否更新以反映城市名称的变化?还是处于破损状态?我还能找到与旧城区相同的地址索引吗?我必须单独更新这个索引吗?我可以调用无操作修饰符来更新索引吗?

// Presume I have the city element from the index.
// Is this supposed to update the city index after it was modified above?

book.get<4>().modify(city_iter, [](t_postal_address &) {});
4

2 回答 2

1

正如@sehe 评论的那样,索引都已更新,因此您很安全。补充观察:你的索引#2

ordered_non_unique<const_mem_fun<const_mem_fun<t_postal_address, string, &t_postal_address::street_name>>

实际上与索引#0(基于复合键的索引)是多余的:您可以只将一个字符串(代表街道名称)传递给索引#0,它的行为与索引#2 完全相同。这为您节省了一些空间和执行时间。它背后的神奇之处在于,当使用复合键时,您可以传递部分信息,直到复合键的第一个元素。

于 2014-04-15T09:23:39.750 回答
1

在我看来应该没有问题:

文档提到:

效果:调用 mod(e),其中 e 是 position 指向的元素,并重新排列*position到 multi_index_container 的所有索引中。如果重排成功

  • 索引不唯一或不存在具有等效键的其他元素,
  • multi_index_container 的所有其他索引都允许 AND 重新排列。如果重新排列失败,则删除该元素。

这意味着所有索引都已更新。请注意,原则上所有索引都可以拒绝编辑。您可能希望使用回滚来防止数据丢失。

于 2014-04-14T23:00:49.567 回答