Re: Any difference in iteration speed

Discussion in 'C++' started by Russell Hanneken, Aug 19, 2003.

  1. Andrew Ward wrote:
    > Between this:
    >
    > vector<int> vi;
    >
    > for(vector<int>::iterator it = vi.begin();it != vi.end();it++) {}
    >
    > and this:
    >
    > vector<int>::iterator end = vi.end();
    > for(vector<int>::iterator it = vi.begin();it != end;it++) {}
    >
    > In other words, does the reapplication of end() slow things down at all?


    In theory, yes. I'm not sure if a code optimizer could smooth over the
    problem.

    > Also, is there any difference between it++ and ++it in this situation?


    Possibly. This is addressed in the FAQ:

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

    std::vector<int>::iterator might just be int *, so there might be no
    difference in this case.

    For what it's worth, I would write the code like this:

    typedef std::vector<int> IntVector;
    IntVector vi;
    for (IntVector::iterator it(vi.begin()), end(vi.end()); it != end; ++it) {}

    Regards,

    Russell Hanneken
     
    Russell Hanneken, Aug 19, 2003
    #1
    1. Advertising

  2. Russell Hanneken

    Me Guest

    "Russell Hanneken" <> wrote in message
    news:fdx0b.239$...
    > For what it's worth, I would write the code like this:
    >
    > typedef std::vector<int> IntVector;
    > IntVector vi;
    > for (IntVector::iterator it(vi.begin()), end(vi.end()); it != end; ++it)

    {}
    >


    Where is "end" defined so that you can use it in the loop like that?
     
    Me, Aug 20, 2003
    #2
    1. Advertising

  3. "Me" <> wrote in message
    news:wqy0b.241784$...
    > "Russell Hanneken" <> wrote in message
    > news:fdx0b.239$...
    > > For what it's worth, I would write the code like this:
    > >
    > > typedef std::vector<int> IntVector;
    > > IntVector vi;
    > > for (IntVector::iterator it(vi.begin()), end(vi.end()); it != end; ++it)
    > > {}

    >
    > Where is "end" defined so that you can use it in the loop like that?


    In the for loop's initialization expression, right after the comma following
    "it(vi.begin())".

    --
    Russell Hanneken
     
    Russell Hanneken, Aug 20, 2003
    #3
  4. "Russell Hanneken" <> wrote in message
    news:0wy0b.289$...
    > "Me" <> wrote in message
    > news:wqy0b.241784$...
    > > "Russell Hanneken" <> wrote in message
    > > news:fdx0b.239$...
    > > > For what it's worth, I would write the code like this:
    > > >
    > > > typedef std::vector<int> IntVector;
    > > > IntVector vi;
    > > > for (IntVector::iterator it(vi.begin()), end(vi.end()); it != end;
    > > > ++it)
    > > > {}

    > >
    > > Where is "end" defined so that you can use it in the loop like that?

    >
    > In the for loop's initialization expression, right after the comma
    > following "it(vi.begin())".


    Er, I should have said "statement" instead of "expression." This part

    IntVector::iterator it(vi.begin()), end(vi.end());

    declares two variables of IntVector::iterator type (it and end). end is
    initialized with the return value of vi.end().

    Since it's a declaration, I guess it can't properly be called an expression.

    --
    Russell Hanneken
     
    Russell Hanneken, Aug 20, 2003
    #4
  5. Me wrote:
    > "Russell Hanneken" <> wrote in message
    > news:fdx0b.239$...
    >
    >>For what it's worth, I would write the code like this:
    >>
    >>typedef std::vector<int> IntVector;
    >>IntVector vi;
    >>for (IntVector::iterator it(vi.begin()), end(vi.end()); it != end; ++it)

    >
    > {}
    >
    >
    > Where is "end" defined so that you can use it in the loop like that?
    >
    >


    The definition is shown in the code you quoted. There are 2 iterators
    declared in the first part of the for loop. One is called 'it' and the
    second is called 'end'. They are initialized with vi.begin() and
    vi.end(), respectively.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
     
    Kevin Goodsell, Aug 20, 2003
    #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. Ham

    I need speed Mr .Net....speed

    Ham, Oct 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,371
    Antony Baula
    Oct 29, 2004
  2. efiedler
    Replies:
    1
    Views:
    2,130
    Tim Ward
    Oct 9, 2003
  3. Matthew Burgess

    Re: Any difference in iteration speed

    Matthew Burgess, Aug 19, 2003, in forum: C++
    Replies:
    3
    Views:
    438
    Axter
    Aug 20, 2003
  4. Rudi
    Replies:
    5
    Views:
    5,306
  5. Sven C. Koehler
    Replies:
    0
    Views:
    138
    Sven C. Koehler
    Oct 2, 2009
Loading...

Share This Page