2012-07-19 23 views
0

好的,所以我首先開始學習C#,現在我正在學習C++,所以我對它很陌生。
在C#中,當我想要訪問列表中的列表時,我只需要使用嵌套for-each循環。如何訪問C++列表中的列表?

現在,因爲在C++中我不知道如何使用for-each循環,我嘗試使用for循環訪問嵌套列表。

這裏是我的代碼:

int main 

{  
    list<list<char *> > moves; 

    list<char *> pointers; 

    list<list<char> > undoValues; 
    list<char> undoValue; 

    for(list<list<char *> >::iterator outer=moves.begin();outer!=moves.end();outer++) 
    { 
     for(list<char *>::iterator inner=outer.begin();inner!=outer.end();inner++) 
     { 

     } 
    } 
} 

我得到2個錯誤:

error 'struct std::_List_iterator<std::list<char*>,std::allocator<char *> > >' has no member named begin 

error 'struct std::_List_iterator<std::list<char*>,std::allocator<char *> > >' has no member named end 

如何訪問嵌套列表?

回答

1

你需要使用(*外)來獲取什麼迭代器指向:

list<char *> pointers; 

list<list<char> > undoValues; 
list<char> undoValue; 
for(list<list<char *> >::iterator outer=moves.begin();outer!=moves.end();outer++) 
{ 
    for(list<char *>::iterator inner=(*outer).begin();inner!=(*outer).end();inner++) 
    { 

    } 
} 
1
for(list<list<char *> >::iterator outer=moves.begin();outer!=moves.end();outer++) 
    for(list<char *>::iterator inner=outer->begin();inner!=outer->end();inner++) 
2

您需要取消引用迭代器去的元素。您可以使用*->

for(list<char *>::iterator inner=outer->begin();inner!=outer->end();inner++) 

for(list<char *>::iterator inner=(*outer).begin();inner!=(*outer).end();inner++) 
+0

+1列出了兩種可能性。 – egrunin 2012-07-19 14:39:09

0

要訪問迭代器指的是,你需要間接對象的成員;所以outer列表的begin()成員是outer->begin(),而不是outer.begin()。同樣適用於end()