tags:

views:

55

answers:

2
struct Group
{
   Group(string _N, set <string> M_)
   {Name = N_; Member = M_}
   string Name;
   set <string> Members;
};

int main()
{
   list <Group> GroupList;
   set <string> Members;

   //collect the members from a file and add to set

   GroupList.pushback(Group(Name, Members));

}

is there a less memory consuming way to the add the members to group instead of passing the whole set to the constructor?

Edit: Renamed the variables starting with underscore.

+1  A: 

By adding an empty element and swapping the set, you avoid the intermediate copy.

GroupList.pushback(Group());
swap(GroupList.back().Members, Members);
Marcus Lindblom
+3  A: 

You can pass a reference and then make a copy and use initialisation list, same with the string.

Group(const string& n, const set <string>& m)
: Name(n), Members(m) {}
stefanB
Doesn't hurt to make them const ref parameters either.
SB
ok, I was going to this at the first place but then i got confused with thinking the reference as a pointer. I thought it would might change the set in GroupList aswell but the variable Members in struct is not a pointer :)
Dave18