Iterators in Java and C++

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    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
  • lbonafide@yahoo.com

    #2
    Re: Iterators in Java and C++

    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?


    Comment

    • James Kanze

      #3
      Re: Iterators in Java and C++

      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:james.kan ze@gmail.com
      Conseils en informatique orientée objet/
      Beratung in objektorientier ter Datenverarbeitu ng
      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

      Comment

      • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

        #4
        Re: Iterators in Java and C++ (ATTN : James Kanze)

        On 2008-04-03 21:38, lbonafide@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

        Comment

        • Razii

          #5
          Re: Iterators in Java and C++

          On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
          <Erik-wikstrom@telia. comwrote:
          >Relevance?
          You have Iterators only when you have containers, like map or vector
          :)


          Comment

          • Razii

            #6
            Re: Iterators in Java and C++

            On Thu, 3 Apr 2008 14:02:01 -0700 (PDT), James Kanze
            <james.kanze@gm ail.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(t hatThing);
            }

            to

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




            Comment

            • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

              #7
              Re: Iterators in Java and C++

              On 2008-04-03 23:12, Razii wrote:
              On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
              <Erik-wikstrom@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

              Comment

              • Sam

                #8
                Re: Iterators in Java and C++

                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)

                iD8DBQBH9W9Cx9p 3GYHlUOIRAhtEAJ 9yGLYN5dp2V0CVA +MROtk8z1qeyACf UWSQ
                a7tzSoCXRqP+oDh GBMsOIKM=
                =uBLO
                -----END PGP SIGNATURE-----

                Comment

                • Lloyd Bonafide

                  #9
                  Re: Iterators in Java and C++

                  Sam <sam@email-scan.comwrote in
                  news:cone.12072 67138.348818.29 934.500@commodo re.email-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.

                  Comment

                  • James Kanze

                    #10
                    Re: Iterators in Java and C++

                    On Apr 3, 11:12 pm, Razii <DONTwhatever.. .@hotmail.comwr ote:
                    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_iterato r 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:james.kan ze@gmail.com
                    Conseils en informatique orientée objet/
                    Beratung in objektorientier ter Datenverarbeitu ng
                    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                    Comment

                    • James Kanze

                      #11
                      Re: Iterators in Java and C++

                      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:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      • Razii

                        #12
                        Re: Iterators in Java and C++

                        On Fri, 4 Apr 2008 01:24:49 -0700 (PDT), James Kanze
                        <james.kanze@gm ail.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( someInitializat ionArguments ) ;
                        ! 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.prin tln(n);
                        }

                        Or

                        List list = getList();

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

                        // Do something else with this element
                        }




                        Comment

                        • Markus Moll

                          #13
                          Re: Iterators in Java and C++

                          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.prin tln(n);
                          }
                          >
                          Or
                          >
                          List list = getList();
                          >
                          for (Object element : list) {
                          ^^^^^^^^^^^^^^^ ^^^^^^
                          .... and here.
                          out.println(ele ment);
                          >
                          // Do something else with this element
                          }
                          Markus

                          Comment

                          • Obnoxious User

                            #14
                            Re: Iterators in Java and C++

                            On Thu, 03 Apr 2008 16:12:59 -0500, Razii wrote:
                            On Thu, 03 Apr 2008 21:03:29 GMT, Erik Wikström
                            <Erik-wikstrom@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

                            Comment

                            • chsalvia@gmail.com

                              #15
                              Re: Iterators in Java and C++

                              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.

                              Comment

                              Working...