Re: Why can't I insert or delete the inside element of set<set<string>>?
In article <1149565235.213388.158660@y43g2000cwc.googlegroups.com>,
Rain Ma <mazhiyu@gmail.com> 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. Why? And how can I
manage to achieve my goal?Thanks.
==============================================================================
======
#include <iostream>
#include <set>
#include <string>
using std::set;
using std::string;
void func()
{
set< set<string> > mySet;
....// Initializing mySet, and it is like {{"Hi", "Hello world
to"}, {"A", "B"}}
set< set<string> >::iterator iter = mySet.begin();
iter->erase("Hi"); //There is a complile error here
}
int main()
{
//.......
return 1;
}
You can't modify an element of the set<...> while it is in the set.
You must make a local copy, erase the entry modify the copy and insert
the modified copy into the set. not very efficient for a set<set<...> >!
if all these copies of complete sets not acceptable perhaps a
set<shared_ptr<set<string> > > will be acceptable in that the inner
sets will be copied when inserted into the outer set originally not
when they are modified. Shared_ptr is in boost or tr1.
The question is really what do you want to do with this
set<set<string> >? Other containers may be a better solution.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]