2010-04-02 64 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; 
}