change std::set comparer functor parameter
You can not change set element value directly although you can do it in
some
platforms(it is not standard).
Yuo can get key compare functor by value,not by refrence or by poniter, so
you can not change the key compare functor's parameter in runtime.
If you can change the element value or the key compare functor, the set's
element ordering is maybe destroyed, and the behavior of the set is maybe
undefined.
But in some spetial cases, I want to change the element value and the key
compare functor synchronously, and I can ensure the set's ordering is
keeped. For instance:
struct mod5_compare
{
bool operator()(int i ,int j) const
{
return (i % 5) < (j % 5);
}
};
std::set<int> iset;
iset.insert(1);
iset.insert(4);
iset.insert(3);
iset.insert(2);// now iset elements oreder:1 2 3 4,comparer functor:
std::less<int>()
std::set<int>::iterator iter = iset.begin();
++iter;//now iter point to 2;
*iter = 6;//can't do that,but assume if
iset.set_key_comp(mod5_compare());//can't do that,but assume if
//now iset elements order is:1 6 3 4,comparer functor: mod5_compare(),and
the result is right
Maybe you think the example is trivial,but I have encountered cases like
that.
So I think this is maybe a limitation for set(and map).
--
HongChao Zhao
TEL: 010-62600953
PHS: 010-81606417
Laboratory of Spatial Information Technology
Division of System Architecture
Institute of Computing Technology
Chinese Academy of Sciences
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]