title: c++: Nested Lists: how to add an item to a list of Lists
body: <p>I am new on the board. I am using MS VS2008.
  I am learning about list of lists. 
  I am trying to add elements to a list which is part of a list of lists. </p>
  
  <p>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. </p>
  
  <p>Any help would be greater appreciated.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>struct item
  {
  string name;
  int age;
  };
  int main()
  {
  list&lt;item&gt; division1;
  list&lt;item&gt; division2;
  
  list&lt; list&lt;item&gt; &gt;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&lt; list&lt;item&gt; &gt;::iterator WholCompIter;
  list&lt;item&gt;::iterator itemIter;
  
  for ( WholCompIter = WholeCompany.begin(); WholCompIter != WholeCompany.end();      WholCompIter++ )
      {
      //incorrect
      //list&lt;item&gt; listEntry = *WholCompIter;
      //instead use:
      list&lt;item&gt; 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-&gt;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
  
          }
      }
  }
      //incorrect. 
  //for(list&lt;item&gt;::iterator i=division2.begin(); i!=division2.end();++i)
  //  cout&lt;&lt;i-&gt;name&lt;&lt;" "&lt;&lt;i-&gt;age&lt;&lt;endl;
      //instead use:
      for ( WholCompIter = WholeCompany.begin(); WholCompIter != WholeCompany.end(); WholCompIter++ )
  {
      list&lt;item&gt;listEntry = *WholCompIter;
  
      for ( itemIter = listEntry.begin(); itemIter != listEntry.end(); itemIter++ )
      {   
      cout&lt;&lt;itemIter-&gt;name&lt;&lt;" "&lt;&lt;itemIter-&gt;age&lt;&lt;endl; 
  
      }
      cout&lt;&lt;endl; cout&lt;&lt;endl;
  }
  return 0;
  }
  </code></pre>