Hi guys, I am new on the board.
I am learning about list of lists.
I am trying to add elements to a list which is part of a list of lists.
I have noted in my program where I am having a problem.
When I debug it, "beth" is being added to the second list (division2), but when the loop is exited, "beth" isn't in the second list even though I have declared all my lists outside of the nested loop.
#include <list>
#include <iostream>
#include <string>
usingnamespace std;
struct item
{
string name;
int age;
};
int main()
{
list<item> division1;
list<item> division2;
list< list<item> >WholeCompany;
item s;
s.name="sandra"; s.age=43; division1.push_back(s);
s.name="Marc"; s.age=19; division2.push_back(s);
s.name="betty"; s.age=34;division2.push_back(s);
WholeCompany.push_back(division1);
WholeCompany.push_back(division2);
list< list<item> >::iterator WholCompIter;
list<item>::iterator itemIter;
for ( WholCompIter = WholeCompany.begin(); WholCompIter != WholeCompany.end(); WholCompIter++ )
{
list<item> listEntry = *WholCompIter;
for ( itemIter = listEntry.begin(); itemIter != listEntry.end(); itemIter++ )
{
//MY ISSUE IS RIGHT HERE! How can I add the value to the list, but the list forgets it when it exit loop
if(itemIter->name =="betty")
{
item s; s.name="beth"; s.age=65;
listEntry.insert(itemIter,s);//problem is here.
//I have also tried
//listEntry.push_back(s) but the output doesn't show it
}
}
}
for(list<item>::iterator i=division2.begin(); i!=division2.end();++i)
cout<<i->name<<" "<<i->age<<endl;
return 0;
}