cpx
cpx

Reputation: 17567

Adding a struct to std::list

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.

Upvotes: 2

Views: 2141

Answers (2)

stefanB
stefanB

Reputation: 79810

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) {}

Upvotes: 3

Macke
Macke

Reputation: 25690

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

GroupList.pushback(Group());
swap(GroupList.back().Members, Members);

Upvotes: 1

Related Questions