Search code examples
c++boostboost-multi-index

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


Solution

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