Iterator = pointer?还是什么? [英] Iterator = pointer? Or what is it?

查看:187
本文介绍了Iterator = pointer?还是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++中的迭代器是指针吗?我问的原因是,似乎没有人完全明白迭代器是什么。这只是他们说的事情或价值。但迭代器只是指向元素,到它的位置。当我们解除引用它时,它就像看一个迭代器指向什么。这是正确的类比吗?请帮助

解决方案

简短的答案是:




  • 指针是一种迭代器。

  • 因此,指针可以用作迭代器。

  • 指针具有迭代器以外的属性。



历史



历史上,我们有C指针,当C ++被发明时C ++。指针表示内存中的位置,因此可以用作数组中的位置。



后来,在20世纪90年代,一个称为迭代器概念的想法被引入C ++。 迭代器概念与称为STL的库(后来被纳入标准库)和称为通用编程的范例相关。迭代器概念来自C指针,用于表示向量 deque 和其他容器中的位置, C指针表示数组中的位置。 迭代器概念经过精心设计,可与C指针兼容,因此我们现在可以说 C指针模型迭代器概念



迭代器概念



一个简单的了解迭代器概念的方法是,如果一个数据类型支持操作和行为的列表,容器,并且允许某种对元素的访问,它可以被称为迭代器。



通过迭代器概念的小心设计, / strong>。因此,指针是一种迭代器。



迭代器概念只是一组对类型的需求,意味着你可以通过C ++的数据抽象能力创建自己的迭代器。 / p>

指针的其他属性



指针具有其他属性,它们与迭代器概念无关。



指针的一个重要用途是表达引用语义,即引用远程内存位置中的一个对象。指针的这种使用以后被认为是不安全的,并且导致发明智能指针。通过比较智能指针和迭代器,我们可以发现它们是完全不相关的概念。



指针的另一个用途是引用原始内存位置。这对于应用程序编程是完全不安全的,但是是微控制器编程操作硬件的必要工具。


Is iterator in C++ a pointer? The reason I ask is that it seems like nobody completely understands what an iterator is. It's just a "thing" or a "value" they say. But iterator simply points to the element, to its position. When we dereferencing it, it's like looking at what an iterator points to. Is it a correct analogy? Please, help

解决方案

The short answer is:

  • Pointer is a kind of iterator.
  • Pointer can be therefore used as an iterator.
  • Pointer has properties other than iterator.

History

Historically, we have C pointer, and it is adapted into C++ when C++ is invented. Pointer represents a location in memory, therefore can be used as a location in an array.

Later, in 1990s, an idea called "iterator concept" is introduced to C++. The "iterator concept" is related to a library called STL (which is later absorbed into the Standard Library) and a paradigm called "generic programming". The iterator concept is inspired from C pointer to represent a location in containers like vector, deque and others, just like how C pointer represent location in array. The iterator concept is carefully engineered to be compatible with C pointer, hence we can nowadays say C pointer models iterator concept.

Iterator concept

A simplified way to understand iterator concept is that, if a data type suppports a list of operations and behaviours, such that it represent a location in a container, and enable some kind of access to the element, it can be called an iterator.

With carefull design of iterator concept, C pointer fulfill that list. Pointer is therefore a kind of iterator.

Iterator concept being just a set of requirement on types, means that you can create your own iterator through C++ power of data abstraction.

Other properties of pointer

Pointer exhibits other properties, and they are not related to iterator concept.

A significant use of pointer is to express reference semantics, i.e. to refer to an object in a remote memory location. This usage of pointer is later considered unsafe, and causes the invention of "smart pointer". By comparing smart pointers and iterators, we can find that they are totally unrelated concepts.

Another use of pointer is to refer to a raw memory location. This is completely unsafe for application programming, but is a essential tool for microcontroller programming to manipulate hardware.

这篇关于Iterator = pointer?还是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆