查找由链接列表链接的数据的各个地址.C++

Finding the individual addresses of data linked by a linked list. C++

本文关键字:链接 地址 C++ 数据 列表 查找      更新时间:2023-10-16

我在从链表中的项目中取回一些地址时遇到了一些问题。与在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来尝试获取迭代器值的地址在内存中的实际指针。如有任何帮助,我们将不胜感激。

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;