STL iterator

Discussion in 'C++' started by Allerdyce.John@gmail.com, Jan 24, 2006.

  1. Guest

    Hi,

    I am reading c++ primer, in the section about iteraor, it has an
    example:

    To iterate over the elements of any container type, we write
    for (iter = container.begin(); iter != container.end(); ++iter)
    do_soemthing_wtih_element (*iter);

    why we write "++iter" instead of "iter++"?
    , Jan 24, 2006
    #1
    1. Advertising

  2. mlimber Guest

    wrote:
    > Hi,
    >
    > I am reading c++ primer, in the section about iteraor, it has an
    > example:
    >
    > To iterate over the elements of any container type, we write
    > for (iter = container.begin(); iter != container.end(); ++iter)
    > do_soemthing_wtih_element (*iter);
    >
    > why we write "++iter" instead of "iter++"?


    See this FAQ:

    http://www.parashift.com/c -faq-lite/operator-overloading.html#faq-13.15

    Cheers! --M
    mlimber, Jan 24, 2006
    #2
    1. Advertising

  3. wrote:
    > I am reading c++ primer, in the section about iteraor, it has an
    > example:
    >
    > To iterate over the elements of any container type, we write
    > for (iter = container.begin(); iter != container.end(); ++iter)
    > do_soemthing_wtih_element (*iter);
    >
    > why we write "++iter" instead of "iter++"?


    This is covered in the FAQ, I believe. See section 13.
    http://www.parashift.com/c -faq-lite/

    V
    Victor Bazarov, Jan 24, 2006
    #3
  4. Mike Wahler Guest

    Re: [FAQ] STL iterator

    <> wrote in message
    news:...
    > Hi,
    >
    > I am reading c++ primer, in the section about iteraor, it has an
    > example:
    >
    > To iterate over the elements of any container type, we write
    > for (iter = container.begin(); iter != container.end(); ++iter)
    > do_soemthing_wtih_element (*iter);
    >
    > why we write "++iter" instead of "iter++"?


    It will give a compiler a better chance of creating
    efficient code. See FAQ for details:
    http://www.parashift.com/c -faq-lite/

    Your question is answered in section 13 (But
    be sure to read the whole FAQ, it contains
    much useful information.)

    -Mike
    Mike Wahler, Jan 24, 2006
    #4
  5. <> wrote in message
    news:...

    > To iterate over the elements of any container type, we write
    > for (iter = container.begin(); iter != container.end(); ++iter)
    > do_soemthing_wtih_element (*iter);
    >
    > why we write "++iter" instead of "iter++"?


    The FAQ covers the efficiency aspect; but to me the reason is simpler than
    that: because we want to point to the next element, nothing else...

    Prefix increment, just increments; and in the case of iterators,
    incrementing is pointing to the next element.

    On the other hand; postfix increment stores the old state of the object,
    increments the object, and returns the stored old state.

    Since the for loop above only deals only with incrementing, the author of
    the code uses prefix increment.

    Ali
    =?iso-8859-1?Q?Ali_=C7ehreli?=, Jan 25, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,420
  2. greg
    Replies:
    6
    Views:
    454
    Dietmar Kuehl
    Jul 17, 2003
  3. Replies:
    6
    Views:
    643
    Jim Langston
    Oct 30, 2005
  4. Steven D'Aprano

    What makes an iterator an iterator?

    Steven D'Aprano, Apr 18, 2007, in forum: Python
    Replies:
    28
    Views:
    1,156
    Steven D'Aprano
    Apr 20, 2007
  5. David Bilsby
    Replies:
    5
    Views:
    2,042
    David Bilsby
    Oct 9, 2007
Loading...

Share This Page