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

编程入门 行业动态 更新时间:2024-10-24 18:21:16
本文介绍了迭代器和指针有什么关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

更多推荐

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

本文发布于:2023-07-29 11:07:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240146.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么关系   指针   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!