2010-04-02 58 views
0
explicit list(
    const A& Al = A() 
); 
explicit list(
    size_type n, 
    const T& v = T(), 
    const A& Al = A() 
); 
list(
    const list& x 
); 
list(
    const_iterator First, 
    const_iterator Last, 
    const A& Al = A() 
); 
+2

此代碼依次打印每個條目 - 您只需要內部循環(不計數 pm100 2010-04-02 15:03:14

+1

我不明白你編輯的目的。你能澄清嗎? – ZoogieZork 2010-04-02 20:41:20

回答

0

至少根據你在這裏得到的結果,問題不在於你如何遍歷列表 - 它完全是使用列表。您要求隨機訪問數據,這意味着您應該使用類似矢量或deque的東西而不是列表。

+0

他在某種意義上實現了隨機訪問,但只是用它來訪問順序中的元素。 – Potatoswatter 2010-04-02 15:09:10

0

我真的說不出什麼,這是試圖做的,但你在你的內循環有可能的段錯誤:

for (cursor = head_ptr; cursor !=NULL ||count<i; cursor=cursor->link()) 
{ 
count++;  
} 

你的終止條件表明,如果count < i,你會不斷循環,即使cursor == NULL ;當執行cursor=cursor->link()時,您會嘗試解除引用NULL

也許你的意思是cursor !=NULL && count<i

1
#include <list> 
using namespace std; 

list<Node> my_list; 

int index = 0; 
for (list<Node>::iterator cursor = my_list.begin(); 
    it!= my_list.end(); ++ cursor, ++ index) { 
    cout << "index: " << index << 「 value: 「 << cursor->data() << endl; 
}