C++能不能输出迭代器保存的地址?
黄舟
黄舟 2017-04-17 14:28:57
0
2
560

比如有如下代码:

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

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

黄舟
黄舟

人生最曼妙的风景,竟是内心的淡定与从容!

reply all(2)
Peter_Zhu

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

黄舟

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template