2013-02-26 40 views
0

我從位於鏈接列表中的項目獲取一些地址時遇到了一些麻煩。而不是在for循環開始時創建和初始化迭代器地址,我想要查看列表中實際數據的地址。這可能沒有創建我自己的列表實現?這裏是我已經得到的代碼塊,但顯然它不是我想要的方式工作。查找由鏈接列表鏈接的數據的各個地址。 C++

int j = 0; 

testIntList.resize(100); 
for (list<int>::iterator i = testIntList.begin(); i != testIntList.end(); ++i) { 
    *i = j*j; 
    int * k = &(*i); 
    cout << "the value of the iterator is " << *i << " and the address of the iterator is " << &i << "and the address of the pointer is " << *k << endl; 
    j++; 
} 

我以這種方式使用k來嘗試獲取迭代器的值的地址的內存中的實際指針。任何幫助將非常感激。

+1

你已經擁有了 - 它是'k'。 – jrok 2013-02-26 23:20:47

回答

2

i是你的迭代器,所以&i將是迭代器的地址 - 不是你想要的。現在,您知道*i是您的迭代器指向的對象,因此該對象的地址是&*i。這就是你在k中輸入的內容。只需打印出k

cout << "the value of the iterator is " << *i 
    << " and the address of the iterator is " << &i 
    << " and the address of the pointer is " << k << endl; 
+0

非常感謝。我會+1,但我剛開始在這裏。 – 2013-03-02 15:09:44