vector iterators ...

Discussion in 'C++' started by ma740988, Jun 3, 2006.

  1. ma740988

    ma740988 Guest

    typedef std::vector < std::complex < double > > complex_vec_type;

    // option1
    int main()
    {
    complex_vec_type cc ( 24000 );
    complex_vec_type dd ( &cc[ 0 ], &cc[ 12000 ] );
    }

    versus

    // option 2
    int main()
    {
    complex_vec_type cc ( 24000 );
    complex_vec_type dd ( cc.begin(), cc.begin()+12000 );
    }

    Does reallocation occur during construction of dd for either option?

    Now if I interpret the standard, more specifically the statement ..

    brigns to mind the question of what are vector iterators? If memory
    serves, they meet requirements of output / input / forward and
    bidirectional iterators. With regards to option 2, I'm assuming no
    reallocation occurs since iterators first and last are 'forward' (
    correct ? ) iterators.

    Things get muddled though with the statement " does at most 2N calls
    to
    the copy constructor of T and logN reallocations if they are just
    input iterators"
    Now vector iterators meet the category of input iterators hence, what
    is 'cc.begin()'? If cc.begin is an input interator then reallocations
    does occur during the construction of dd. I'm a little confused.
    ma740988, Jun 3, 2006
    #1
    1. Advertising

  2. ma740988 wrote:
    > typedef std::vector < std::complex < double > > complex_vec_type;
    >
    > // option1
    > int main()
    > {
    > complex_vec_type cc ( 24000 );
    > complex_vec_type dd ( &cc[ 0 ], &cc[ 12000 ] );
    > }
    >
    > versus
    >
    > // option 2
    > int main()
    > {
    > complex_vec_type cc ( 24000 );
    > complex_vec_type dd ( cc.begin(), cc.begin()+12000 );
    > }
    >
    > Does reallocation occur during construction of dd for either option?


    No.

    > Now if I interpret the standard, more specifically the statement ..
    >
    >
    >
    > brigns to mind the question of what are vector iterators? If memory
    > serves, they meet requirements of output / input / forward and
    > bidirectional iterators. With regards to option 2, I'm assuming no
    > reallocation occurs since iterators first and last are 'forward' (
    > correct ? ) iterators.


    They are random access iterators.

    > Things get muddled though with the statement " does at most 2N calls
    > to
    > the copy constructor of T and logN reallocations if they are just
    > input iterators"
    > Now vector iterators meet the category of input iterators hence, what
    > is 'cc.begin()'? If cc.begin is an input interator then reallocations
    > does occur during the construction of dd. I'm a little confused.


    Random access iterators certainly meet the requirements for input
    iterators but they most definitely are not "just input iterators". So
    this paragraph does not apply.
    Markus Schoder, Jun 3, 2006
    #2
    1. Advertising

  3. ma740988

    Andre Kostur Guest

    On Sat, 03 Jun 2006 08:51:23 -0700, ma740988 wrote:

    > typedef std::vector < std::complex < double > > complex_vec_type;
    >
    > // option1
    > int main()
    > {
    > complex_vec_type cc ( 24000 );
    > complex_vec_type dd ( &cc[ 0 ], &cc[ 12000 ] );
    > }
    >
    > versus
    >
    > // option 2
    > int main()
    > {
    > complex_vec_type cc ( 24000 );
    > complex_vec_type dd ( cc.begin(), cc.begin()+12000 );
    > }
    >
    > Does reallocation occur during construction of dd for either option?


    No. You get one allocation of sufficient size at the beginning, since the
    compiler can know how many elements there will be.

    >
    > Now if I interpret the standard, more specifically the statement ..
    >
    >
    >
    > brigns to mind the question of what are vector iterators? If memory
    > serves, they meet requirements of output / input / forward and
    > bidirectional iterators. With regards to option 2, I'm assuming no


    They're also RandomAccess iterators too.

    > reallocation occurs since iterators first and last are 'forward' ( correct
    > ? ) iterators.


    RandomAccess.

    >
    > Things get muddled though with the statement " does at most 2N calls to
    > the copy constructor of T and logN reallocations if they are just input
    > iterators"
    > Now vector iterators meet the category of input iterators hence, what is
    > 'cc.begin()'? If cc.begin is an input interator then reallocations does
    > occur during the construction of dd. I'm a little confused.


    Note that it said *just* input iterators. Since vector iterators are
    "more" than just input iterators (RandomAccess, to be specific), the
    compiler knows how many elements that there will be, and will size the new
    vector appropriately.

    If we're dealing with only an input iterator (such as istream_iterator),
    the compiler cannot determine how many elements there will be before
    starting to copy. With forward iterators, the compiler can safely advance
    through the range to count how many elements there will be, then start
    again at the first iterator. With input iterators, the compiler cannot
    "rewind" the range to get back to the beginning, so it must insert the
    elements into the new vector as it is going through the range the first
    time around.
    Andre Kostur, Jun 3, 2006
    #3
    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. pmatos
    Replies:
    6
    Views:
    23,727
  2. Marcin Kaliciñski

    Iterators and reverse iterators

    Marcin Kaliciñski, May 8, 2005, in forum: C++
    Replies:
    1
    Views:
    475
    Kai-Uwe Bux
    May 8, 2005
  3. David Crawford

    Question on vector of vector iterators

    David Crawford, Dec 15, 2005, in forum: C++
    Replies:
    3
    Views:
    468
  4. Replies:
    8
    Views:
    1,890
    Csaba
    Feb 18, 2006
  5. , India
    Replies:
    10
    Views:
    1,059
    James Kanze
    Aug 8, 2009
Loading...

Share This Page