0

我在从链接列表中的项目中获取一些地址时遇到了一些麻烦。而不是在 for 循环开始时创建和初始化的迭代器地址,我想获取位于列表中的实际数据的地址。如果不创建我自己的列表实现,这可能吗?这是我得到的代码块,但显然它没有按照我想要的方式工作。

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 来尝试获取内存中迭代器值地址的实际指针。任何帮助将不胜感激。

4

1 回答 1

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;
于 2013-02-26T23:20:13.783 回答