Home  >  Q&A  >  body text

C++能不能输出迭代器保存的地址?

比如有如下代码:

vector<int>::iterator tmp = find(box.begin(), box.end(), 0);

我想知道tmp保存的地址是多少,应该怎么做呢?

黄舟黄舟2764 days ago521

reply all(2)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 14:30:57

    Generally speaking, an iterator is a data structure that stores the real data address in an internal member variable and overloads various operators (such as the dereference operator *). To obtain the data address, the recommended method is &*, that is, first obtain the real data through the asterisk operator which may be overloaded by and then obtain the address.

    But for vector<int>, it is actually typedef int *iterator, so even (int*)tmp is no problem.

    You can refer to "STL Source Code Analysis" translated by Hou Jie

    reply
    0
  • 黄舟

    黄舟2017-04-17 14:30:57

    &*tmp is enough, or std::addressof(*tmp)

    reply
    0
  • Cancelreply