473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Iterators in Java and C++

This is a little bit off topic but I hop you'll forgive me.

A few days ago some expressed the opinion (in a post I can't find, but
it was probably in one of Razii's threads) that Java's iterators were
better than C++ iterators, or at least that the Java iterator concept
was better (or something to that effect). I would be interested to hear
about why whoever wrote it feels that way.

--
Erik Wikström
Apr 3 '08 #1
15 1880
On Apr 3, 2:12*pm, Erik Wikström <Erik-wikst...@telia.comwrote:
This is a little bit off topic but I hop you'll forgive me.

A few days ago some expressed the opinion (in a post I can't find, but
it was probably in one of Razii's threads) that Java's iterators were
better than C++ iterators,
You mean this?

http://tinylink.com/?iaOmlBsgBy
Apr 3 '08 #2
On 3 avr, 21:12, Erik Wikström <Erik-wikst...@telia.comwrote:
This is a little bit off topic but I hop you'll forgive me.
A few days ago some expressed the opinion (in a post I can't
find, but it was probably in one of Razii's threads) that
Java's iterators were better than C++ iterators, or at least
that the Java iterator concept was better (or something to
that effect). I would be interested to hear about why whoever
wrote it feels that way.
That was me, and the reason is simply: you don't need two of
them. Try chaining functions which use iterators, for example.
Or writing a filtering iterator.

Not that Java's iterators are perfect, either. The merge access
and incrementing---as did the USL iterators. By the time Java
was being developed, we'd already established that this wasn't a
good idea, so it's hard to understand why they did it.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 3 '08 #3
On 2008-04-03 21:38, lb*******@yahoo.com wrote:
On Apr 3, 2:12 pm, Erik Wikström <Erik-wikst...@telia.comwrote:
>This is a little bit off topic but I hop you'll forgive me.

A few days ago some expressed the opinion (in a post I can't find, but
it was probably in one of Razii's threads) that Java's iterators were
better than C++ iterators,

You mean this?

http://tinylink.com/?iaOmlBsgBy
Yes, thanks.

James, can you explain what you meant by "... for most everyday jobs
like this, in fact, the Java collections library (and especially the
concept of iterators in Java) is far superior to the STL."?

While it was some time ago since I last used Java (before generics) I've
always liked the way the STL iterators, it is a quite simple concept but
still very powerful. To my knowledge there is not easy way to perform
operations on a subset of a container using Java's iterators.

--
Erik Wikström
Apr 3 '08 #4
On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
<Er***********@telia.comwrote:
>Relevance?
You have Iterators only when you have containers, like map or vector
:)
Apr 3 '08 #5
On Thu, 3 Apr 2008 14:02:01 -0700 (PDT), James Kanze
<ja*********@gmail.comwrote:
>Not that Java's iterators are perfect, either. The merge access
and incrementing---as did the USL iterators. By the time Java
was being developed, we'd already established that this wasn't a
good idea, so it's hard to understand why they did it.

I am not sure what you mean but with with 1.5, the syntax changed from

for(Iterator lineup = list.iterator() ; lineup.hasNext() ; ) {
Object thatThing = lineup.next();
myMonster.eat(thatThing);
}

to

for(Object thatThing : list) {
myMonster.eat(thatThing);
}


Apr 3 '08 #6
On 2008-04-03 23:12, Razii wrote:
On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
<Er***********@telia.comwrote:
>>Relevance?

You have Iterators only when you have containers, like map or vector
:)
And?
Read my post again and tell me how your response is relevant.

--
Erik Wikström
Apr 3 '08 #7
Sam
Erik Wikström writes:
This is a little bit off topic but I hop you'll forgive me.

A few days ago some expressed the opinion (in a post I can't find, but
it was probably in one of Razii's threads) that Java's iterators were
better than C++ iterators, or at least that the Java iterator concept
was better (or something to that effect). I would be interested to hear
about why whoever wrote it feels that way.
This is stupid. This is like saying that apples are better than oranges.
Some people like apples more than oranges, other people like oranges more
than apples. There is no quantifiable way to compare apples and oranges on
some nebulous, global, "better" scale.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQBH9W9Cx9p3GYHlUOIRAhtEAJ9yGLYN5dp2V0CVA+MROt k8z1qeyACfUWSQ
a7tzSoCXRqP+oDhGBMsOIKM=
=uBLO
-----END PGP SIGNATURE-----

Apr 4 '08 #8
Sam <sa*@email-scan.comwrote in
news:co******************************@commodore.em ail-scan.com:
Erik Wikström writes:
>This is a little bit off topic but I hop you'll forgive me.

A few days ago some expressed the opinion (in a post I can't find,
but it was probably in one of Razii's threads) that Java's iterators
were better than C++ iterators, or at least that the Java iterator
concept was better (or something to that effect). I would be
interested to hear about why whoever wrote it feels that way.

This is stupid. This is like saying that apples are better than
oranges. Some people like apples more than oranges, other people like
oranges more than apples. There is no quantifiable way to compare
apples and oranges on some nebulous,
Right, there is no quantifiable way to compare the taste of apples and
oranges, nor is there a way to convince someone that one is better than
another. With programming constructs or paradigms, however, there are
often tradeoffs in choosing one over another, and reasons for choosing
one over another can be concrete.
Apr 4 '08 #9
On Apr 3, 11:12 pm, Razii <DONTwhatever...@hotmail.comwrote:
On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
<Erik-wikst...@telia.comwrote:
Relevance?
You have Iterators only when you have containers, like map or
vector :)
Since when? C++ has istream_iterator in the standard, and Boost
has a lot of iterators which don't depend on an underlying
container. And you can easily do the same thing in Java---much
more easily, in fact. (The fact that they are so easy to
implement may account for why no one felt it necessary to
provide them as standard, or as part of a widely used library.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Apr 4 '08 #10
On Apr 4, 1:58 am, Sam <s...@email-scan.comwrote:
Erik Wikström writes:
This is a little bit off topic but I hop you'll forgive me.
A few days ago some expressed the opinion (in a post I can't find, but
it was probably in one of Razii's threads) that Java's iterators were
better than C++ iterators, or at least that the Java iterator concept
was better (or something to that effect). I would be interested to hear
about why whoever wrote it feels that way.
This is stupid. This is like saying that apples are better
than oranges.
No. You're not comparing things in an absolute. If you call
something an "iterator", it is because it fulfills a specific
purpose. Implicit in the statement is that "Java iterators are
better iterators than C++ iterators". Sort of like "oranges are
a better source of vitamin C than apples".

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Apr 4 '08 #11
On Fri, 4 Apr 2008 01:24:49 -0700 (PDT), James Kanze
<ja*********@gmail.comwrote:
>And that's the problem. You should be able to access the object
without advancing the iterator. Most logically, advancing the
iterator should be the third part of the if.

The standard "pattern" is:

for ( Iterator iter( someInitializationArguments ) ;
! iter.isDone() ;
iter.next() ) {
doSomethingWith( iter.element() ) ;
}
If the problem is that "You should be able to access the object
without advancing the iterator", where am I calling next in this
syntax.

int[] nums = { 1, 2, 3, 4, 5, 6 };

for(int n : nums) {
System.out.println(n);
}

Or

List list = getList();

for (Object element : list) {
out.println(element);

// Do something else with this element
}


Apr 4 '08 #12
Hi

Razii wrote:
If the problem is that "You should be able to access the object
without advancing the iterator", where am I calling next in this
syntax.
Easy...
int[] nums = { 1, 2, 3, 4, 5, 6 };

for(int n : nums) {
^^^^^^^^^^^^

Here...
System.out.println(n);
}

Or

List list = getList();

for (Object element : list) {
^^^^^^^^^^^^^^^^^^^^^
.... and here.
out.println(element);

// Do something else with this element
}
Markus

Apr 4 '08 #13
On Thu, 03 Apr 2008 16:12:59 -0500, Razii wrote:
On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
<Er***********@telia.comwrote:
>>Relevance?

You have Iterators only when you have containers, like map or vector :)
No, you don't. I even use custom iterators for a lot more than
simple containers.

--
OU
Apr 4 '08 #14
Overall, I usually prefer C++ iterators to Java style iterators for
subjective, aesthetic reasons. However, there are times when I
question the sanity of having two iterator objects instead of one.
Sam pointed out that Java iterators are much less efficient
constructs, (using virtual functions and frequent reallocation), but
the issue here I think is not so much the actual implementation, but
the interface. In other words, would it be better to implement Java-
style iterator constructs in C++?

Especially, in a case where you have a rather complex iterator object
that has a lot of member variables, and so takes up a lot more stack
space than a simple pointer. Usually, the "end" iterator object is
only there so you can compare a single member variable. The rest of
the member variables in the "end" iterator object are usually unused.
While there's probably not any measurable performance loss, the Java-
style interface would me more efficient in theory, because you
wouldn't have the extra "end" iterator with all the useless member
variables it stores.
Jun 27 '08 #15
In article <9c3fd6d1-6eb0-4691-a79c-059960377100
@q10g2000prf.googlegroups.com>, ch******@gmail.com says...
Overall, I usually prefer C++ iterators to Java style iterators for
subjective, aesthetic reasons. However, there are times when I
question the sanity of having two iterator objects instead of one.
C++ 0x should be somewhat more to your liking then -- it introduces
Ranges. A range is essentially equivalent to a pair of iterators; it
represents a range of objects (typically, but not necessarily, in a
container).

[ ... ]
Especially, in a case where you have a rather complex iterator object
that has a lot of member variables, and so takes up a lot more stack
space than a simple pointer. Usually, the "end" iterator object is
only there so you can compare a single member variable. The rest of
the member variables in the "end" iterator object are usually unused.
While there's probably not any measurable performance loss, the Java-
style interface would me more efficient in theory, because you
wouldn't have the extra "end" iterator with all the useless member
variables it stores.
True. I think an iterator containing a lot of data is fairly unusual,
but if/when you need to create an iterator that contains a lot of data
that's never used, that's obviously not particularly efficient.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: adeleinandjeremy | last post by:
I am taking a second programming course in Java and am currently attempting to apply what I have learned using Python instead. One thing that is puzzling me is how to use an iterator. I am...
3
by: Adelein and Jeremy | last post by:
I am taking a second programming course in Java and am currently attempting to apply what I have learned using Python instead. One thing that is puzzling me is how to use an iterator. I am...
7
by: cppaddict | last post by:
I'm running a Java application that uses a native C++ library I've written. The Java application hits one of the native methods pretty frequently (about 100 ms or so), and while it usually works...
18
by: deancoo | last post by:
I have gotten into the habit of often using copy along with an insert iterator. There are scenarios where I process quite a lot of data this way. Can someone give me a general feel as to how much...
3
by: Christian Christmann | last post by:
Hi, I've some problems with iterators on hashtbales. My hashtable.h class HashTable { map<int, void*> nhash; map<int, void*> getNhash();
2
by: ma740988 | last post by:
typedef std::vector < std::complex < double > > complex_vec_type; // option1 int main() { complex_vec_type cc ( 24000 ); complex_vec_type dd ( &cc, &cc ); } versus
8
by: pvonnied | last post by:
Hi, Once more a question (sorry for the different e-mail addresses , here @ work we have to use Google to post to newsgroups): If I use an iterator, say from std::map, I initialize it like so:...
18
by: desktop | last post by:
1) I have this code: std::list<intmylist; mylist.push_back(1); mylist.push_back(2); mylist.push_back(3); mylist.push_back(4);
13
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.