迭代器和指针有什么关系?

How are iterators and pointers related?(迭代器和指针有什么关系?)
本文介绍了迭代器和指针有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

带有迭代器的代码看起来很像带有指针的代码.迭代器属于某种晦涩的类型(例如 std::vector::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?

解决方案

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.

这篇关于迭代器和指针有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

Could I ever want to access the address zero?(我可以想访问地址零吗?)
C++ Access derived class member from base class pointer(C++ 从基类指针访问派生类成员)
Weird Behaviour with const_cast(const_cast 的奇怪行为)
Are pointer variables just integers with some operators or are they quot;symbolicquot;?(指针变量只是带有某些运算符的整数还是“符号?)
Modifying a char *const string(修改 char *const 字符串)
Modifying a const int in C++(修改 C++ 中的 const int)