C++ 迭代器和指针是如何关联的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2728190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How are iterators and pointers related?
提问by sharptooth
Code with iterators looks pretty much like code with pointers. Iterators are of some obscure type (like std::vector<int>::iterator
for example).
带有迭代器的代码看起来很像带有指针的代码。迭代器是一些晦涩的类型(std::vector<int>::iterator
例如)。
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?
我不明白的是迭代器和指针是如何相互关联的 - 迭代器是一个指针的包装器,带有重载操作以前进到相邻元素还是其他东西?
回答by Tristram Gr?bener
Iterators are a generalization of pointers.
迭代器是指针的泛化。
An iterator (depending on the variants) have to implement * and ++
迭代器(取决于变体)必须实现 * 和 ++
So a pointer IS an iterator. But not necessarily the other way round.
所以指针是一个迭代器。但不一定反过来。
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.
如果你想迭代一个复杂的结构(一棵树,一个图......),迭代器将不仅仅是一个指针,并且不会对内存中的某个实际位置进行任何引用。
回答by UncleBens
Iterators are objects that overload certain operators, so the usage would look like they were pointers. That's within the capabilities of a given iterator category. Random access iterators look entirely like pointers, other types of iterators don't provide some operations (e.g list<X>::iterator
which is bidirectional doesn't have operator +=
among many others that would require random access).
迭代器是重载某些运算符的对象,因此其用法看起来就像是指针。这在给定迭代器类别的能力范围内。随机访问迭代器看起来完全像指针,其他类型的迭代器不提供一些操作(例如list<X>::iterator
,双向迭代器+=
在需要随机访问的许多其他迭代器中没有运算符)。
As to the "obscure names", it is not completely unthinkable to use a plain pointer for an iterator:
至于“晦涩的名字”,对迭代器使用普通指针并非完全不可想象:
template <class T>
class MyContainer
{
...
typedef T* iterator;
}
MyContainer<int>::iterator it; //the type is really int*
回答by Alex Budovski
Conceptually, yes -- but they need not be pointers. Their internals and capabilities will depend on the data structure they "wrap".
从概念上讲,是的——但它们不必是指针。它们的内部结构和功能将取决于它们“包装”的数据结构。
That is why there are different "classes" of iterators. E.g. Unidirectional, Bidirectional, RandomAccess, etc.
这就是迭代器有不同“类”的原因。例如单向、双向、随机访问等。
Some are capable of multiple classes.
有些能够进行多个类。
E.g. if the internal structure is a Red-Black tree or Linked List, the iterators might be Bidirectional, but not RandomAccess. If they wrap a vector (implemented as an array), you'll have RandomAccess and Bidirectional.
例如,如果内部结构是红黑树或链表,则迭代器可能是双向的,但不是 RandomAccess。如果它们包装了一个向量(实现为一个数组),您将拥有 RandomAccess 和 Bidirectional。
回答by JoeG
An iterator is just a type that provides the interface required for iterators - these are different for the different types of iterators and are specified in section 24.1 of the C++ standard (Iterator Requirements).
迭代器只是一种提供迭代器所需接口的类型 - 这些对于不同类型的迭代器是不同的,并且在 C++ 标准的第 24.1 节(迭代器要求)中指定。
How iterators are implemented is dependent on what they iterate over - for vectors they are commonly a wrapper around a single pointer to an array (in release builds anyway), for more complex containers they have a more complicated implementation. For open ended ranges they will contain the state of whatever algorithm is beimng used to generate the elements.
迭代器的实现方式取决于它们迭代的内容 - 对于向量,它们通常是指向数组的单个指针的包装器(无论如何在发布版本中),对于更复杂的容器,它们具有更复杂的实现。对于开放式范围,它们将包含用于生成元素的任何算法的状态。
Note that a pointer to an element in an array meets the requirements of a random access iterator, so to some extent they are interchangeable.
请注意,指向数组中元素的指针满足随机访问迭代器的要求,因此在某种程度上它们是可以互换的。