Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8296025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:49:08+00:00 2026-06-08T14:49:08+00:00

I am new on the board. I am using MS VS2008. I am learning

  • 0

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.

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.

Any help would be greater appreciated.

Here is the code:

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++ )
    {
    //incorrect
    //list<item> listEntry = *WholCompIter;
    //instead use:
    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

        }
    }
}
    //incorrect. 
//for(list<item>::iterator i=division2.begin(); i!=division2.end();++i)
//  cout<<i->name<<" "<<i->age<<endl;
    //instead use:
    for ( WholCompIter = WholeCompany.begin(); WholCompIter != WholeCompany.end(); WholCompIter++ )
{
    list<item>listEntry = *WholCompIter;

    for ( itemIter = listEntry.begin(); itemIter != listEntry.end(); itemIter++ )
    {   
    cout<<itemIter->name<<" "<<itemIter->age<<endl; 

    }
    cout<<endl; cout<<endl;
}
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T14:49:10+00:00Added an answer on June 8, 2026 at 2:49 pm

    You’re just creating a copy here:

    list<item> listEntry = *WholCompIter;
    

    Instead, you need a reference:

    list<item>& listEntry = *WholCompIter;
    

    Also, you are printing from division2 in the loop at the bottom. Remember that WholeCompany is only a copy of division2 at the initialization. Since you’ve modified WholeCompany via the reference above, you need to access WholeCompany in the output loop at the bottom.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Some time ago, I've got a new single board computer running Debian which will
I'm new to microcontroller programming and I have interfaced my microcontroller board to another
New to PHP and MySQL, have heard amazing things about this website from Leo
I am new to using matplotlib. I am trying to create a 2D grid,
I'm trying to create a go board using opengl. To do this, I'm trying
I'm trying to create a chess game board with 8x8 buttons. I'm using nested
I have a board with an ATMEGA88 chip which has a bootloader at address
I still very new using Subversion. Is it possible to have a working copy
I have an application which uses a main story board to include a Navigation
I'm using CakePHP 1.3, and trying to make a simple message posting board with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.