mkmostafa
mkmostafa

Reputation: 3171

The difference between modify and modify_key in boost multi_index_container

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

Upvotes: 3

Views: 2886

Answers (2)

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;});

Upvotes: 5

mkmostafa
mkmostafa

Reputation: 3171

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.

Upvotes: 0

Related Questions