C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

change std::set comparer functor parameter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
zade_news_cn99
Guest





PostPosted: Wed Nov 01, 2006 8:20 am    Post subject: change std::set comparer functor parameter Reply with quote



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! ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.