迭代器和指针如何相关? [英] How are iterators and pointers related?
问题描述
带有迭代器的代码看起来很像带指针的代码。迭代器是一些不明确的类型(例如 std :: vector< int> :: iterator
)。
Code with iterators looks pretty much like code with pointers. Iterators are of some obscure type (like std::vector<int>::iterator
for example).
我没有得到的是迭代器和指针是如何相互关联的 - 是一个迭代器一个指针的重载操作提前到相邻的元素的包装器或者是别的什么?
What I don't get is how iterators and pointer are related to each other - is an iterator a wrapper around a pointer with overloaded operations to advance to adjacent elements or is it something else?
推荐答案
迭代器是指针的泛化。
迭代器必须实现* +
An iterator (depending on the variants) have to implement * and ++
因此指针是一个迭代器。
So a pointer IS an iterator. But not necessarily the other way round.
如果你想迭代一个复杂的结构(树,图...),迭代器将远远超过一个指针,并且不会对ram中的某个实际位置进行任何引用。
If you want to iterate over a complex structure (a tree, a graph...), the iterator will be much more than a pointer, and doesn't make any reference to some actual place in the ram.
这篇关于迭代器和指针如何相关?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!