c ++:嵌套列表:如何将项添加到列表列表中

时间:2012-07-30 05:08:00

标签: c++ list nested-lists

我是董事会的新人。我正在使用MS VS2008。 我正在学习列表清单。 我正在尝试将元素添加到列表中,列表是列表列表的一部分。

我在程序中注意到我遇到了问题。 当我调试它时,“beth”被添加到第二个列表(division2),但是当退出循环时,“beth”不在第二个列表中,即使我已经在嵌套循环之外声明了所有列表。

任何帮助都会受到更多的赞赏。

以下是代码:

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;
}

2 个答案:

答案 0 :(得分:2)

您只需在此处创建副本

list<item> listEntry = *WholCompIter;

相反,您需要一个参考:

list<item>& listEntry = *WholCompIter;

此外,您正在底部的循环中从division2打印。请记住,WholeCompany只是初始化时division2的副本。由于您已通过上述参考修改了WholeCompany,因此您需要在底部的输出循环中访问WholeCompany

答案 1 :(得分:1)

修正。

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

        }
    }
}

当您输入list<item> listEntry时,您会创建本地变量,然后在您的代码中将其分配给list<list<item>>::iterator::operator *复制返回值,当您更改它,对您的list WholeCompany没有任何影响。

在字符串中

WholeCompany.push_back(division1);
WholeCompany.push_back(division2);

您推进wholeCompanydivision1的{​​{1}} 副本,因此,当您更改division2的内容时,包含{{1} }}或WholeCompany 不会更改