Re: Why can't I insert or delete the inside element of set<set<string>>?
Rain Ma wrote:
Hi, I was using the STL set as follows and what I want to do here is to
change mySet from {{"Hi", "Hello world to"}, {"A", "B"}} to {{"Hello
world to"},{"A","B"}}, but it fails to compile.
[...]
set< set<string> >::iterator iter = mySet.begin();
iter->erase("Hi"); //There is a complile error here
std::set sorts its elements by their content (or, rather by the order the
second template parameter mandates). In order to maintain internal
consistency, you must not modify its contents because that could mess up
the sorting order without std::set even knowing about it. Therefore,
iterators and const_iterators are the same for sets.
Typically, you would replace an element instead of modifying it, thereby
forcing std::set to sort it into the right position.
Of course, you could also consider using a different container, but that
purely depends on your use case, so it's hard to give advise which one and
how.
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]