1

私は理事会に参加したばかりです。MS VS2008 を使用しています。リストのリストについて学んでいます。リストのリストの一部であるリストに要素を追加しようとしています。

プログラムのどこに問題があるかを指摘しました。デバッグすると、「beth」が 2 番目のリスト (division2) に追加されますが、ループを終了すると、ネストされたループの外側ですべてのリストを宣言したにもかかわらず、「beth」は 2 番目のリストにありません。

どんな助けでも大歓迎です。

コードは次のとおりです。

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

2 に答える 2

2

ここでコピーを作成しているだけです。

list<item> listEntry = *WholCompIter;

代わりに、参照が必要です。

list<item>& listEntry = *WholCompIter;

division2また、下部のループから印刷しています。これは初期化時のWholeCompanyコピーにすぎないことに注意してください。division2上記のリファレンスを介して変更したため、下部の出力ループWholeCompanyにアクセスする必要があります。WholeCompany

于 2012-07-30T05:11:14.697 に答える
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);

とのwholeCompany コピーをプッシュするので、contains ofを変更すると、contains ofが変更されるか、変更されませんdivision1division2WholeCompanydivision1division2

于 2012-07-30T05:11:46.233 に答える