3

What is the difference between modify and modify_key in boost multi_index_container. I read their documentation both and I can't seem to find the difference between the usages of both.

Link to the documentation

Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
mkmostafa
  • 3,071
  • 2
  • 18
  • 47

2 Answers2

4

modify_key is a variation of modify that saves you some typing when the only part of the element you want to change is the key itself. For instance, if I define a multi_index_container such as:

struct element
{
  int x;
  int y;
};

using namespace boost::multi_index;

using container=multi_index_container<
  element,
  indexed_by<
    ordered_unique<member<element,int,&element::x>>
  >
>;

container c=...;

Then the following:

auto it=...;
c.modify(it,[](element& e){e.x=3;});

can be written with modify_key as

auto it=...;
c.modify_key(it,[](int& x){x=3;});
Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
  • Thanks for your answer. I have already figured this out as I stated in my answer. Accepted for the detailed reply. – mkmostafa Feb 16 '16 at 10:43
0

Basically the difference between the usages of both (as far as I understood is as follows):

  • Modify:

    The functor is passed a reference to the whole object that was retrieved and the functor can modify any of the members of this retrieved object.

  • Modify_Key:

    The functor modifies only the key that is used in searching for and retrieving the object. For instance, using an index with the name member of a class to search the container, upon applying modify_key on the returned iterator, the name member will be changed.

Basically modify_key is a special case from modify.

mkmostafa
  • 3,071
  • 2
  • 18
  • 47