How are iterators and pointers related?(迭代器和指针有什么关系?)
问题描述
带有迭代器的代码看起来很像带有指针的代码.迭代器属于某种晦涩的类型(例如 std::vector).
我不明白的是迭代器和指针是如何相互关联的——迭代器是一个指针的包装器,带有重载操作以前进到相邻元素还是其他东西?
迭代器是指针的泛化.
迭代器(取决于变体)必须实现 * 和 ++
所以指针是迭代器.但不一定反过来.
如果你想迭代一个复杂的结构(一棵树、一个图......),迭代器将不仅仅是一个指针,并且不会对内存中的某个实际位置进行任何引用.>
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.
这篇关于迭代器和指针有什么关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:迭代器和指针有什么关系?
基础教程推荐
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 初始化列表*参数*评估顺序 2021-01-01
- CString 到 char* 2021-01-01
