Java和C ++中的迭代器有什么区别? [英] What is the difference between iterators in Java and C++?

查看:448
本文介绍了Java和C ++中的迭代器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

在当前C ++(97)标准中,Iterator在Java中的实现与C ++中的不同库(特别是以前称为STL的部分)定义非常接近C指针(包括算术)的迭代器形式。因此,他们只是指向某个地方。为了有用,通常需要两个指针,以便可以在它们之间进行迭代。我理解C ++ 0x引入的行为更像Java迭代器的范围。



Java引入了 Iterator code> ListIterator )接口在1.2,主要接管更详细的枚举。 Java没有指针运算,所以没有必要像一个指针。他们有一个 hasNext 方法,看看他们是否走到最后,而不是需要两个迭代器。缺点是他们不太灵活。系统需要的方法如 subList ,而不是两个迭代器之间的迭代是包含列表中的特定点。



差异在于,C ++通过模板使用静态多态性,Java使用接口和常见的动态多态性。



迭代器的概念是提供胶水分离算法(真正的控制流)和数据容器。两种方法都做得相当好。在理想情况下,正常代码应该几乎看不到迭代器。


How is the implementation of Iterator in Java different from that in C++?

解决方案

In the current C++ (97) standard library (in particular the portion formerly known as STL) defines a form of iterators that are very close to C pointers (including arithmetic). As such they just point somewhere. To be useful, you generally need two pointers so that you can iterate between them. I understand C++0x introduces ranges which act more like Java iterators.

Java introduced the Iterator (and ListIterator) interface in 1.2, largely taking over from the more verbose Enumerable. Java has no pointer arithmetic, so there is no need to behave like a pointer. They have a hasNext method to see if they have go to the end, instead of requiring two iterators. The downside is that they are less flexible. The system requires methods as subList rather than iterating between two iterators are specific points in the containing list.

A general difference in style is that whereas C++ use "static polymorphism" through templates, Java uses interfaces and the common dynamic polymorphism.

The concept of iterators is to provide the glue to allow separation of "algorithm" (really control flow) and data container. Both approaches do that reasonably well. In ideal situations "normal" code should barely see iterators.

这篇关于Java和C ++中的迭代器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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