
Jeff Flinn wrote:
It would be nice to have a modify_range/modify_key_range that would apply a function object to each deref'd iterator in the range. Since the multi_index iterator derefences to a const value_type&, one can not use std::for_each in particular to apply modifications. Or am I missing something.
Hello Jeff,
I think it is possible to have what you want
without requiring that a new facility be provided
by Boost.MultiIndex itself. For you can write:
template
multi_index_type;
multi_index_type m;
for(int i=0;i<10;++i)m.insert(i);
modify_range(m,m.begin(),m.end(),adder(-2));
or with Boost.Lambda if you prefer:
modify_range(m,m.begin(),m.end(),_1-=2);
There is a problem, though: consider the following:
// buggy call ro modify_range
modify_range(m,m.begin(),m.end(),adder(2));
What's the problem with this? Well, after *increasing*
the value of an element, this is repositioned
further to the end of the container, and consequentely
modify_range will visit it again and engage in a
neverending loop (modulo overflows.) For this situations
you can use a special (albeit slower) variation that
records the range before starting modifying the
elements:
template