STL vector question

Discussion in 'C++' started by Jessica, Jun 25, 2003.

  1. Jessica

    Jessica Guest

    Hi,
    I do not have a lot of experience with STL and I hope some of you
    might be able to help me on this seemingly elementary question.

    I have a vector of doubles (v1). I am trying to copy the values to
    a 2D vector, of which every vector has the same length. I tried the
    following but I get a "System.NullReferenceException" error when I ran
    it.


    cycle = 2;
    n = 12;

    vector< vector<double> >v2D;
    v2D.reserve(cycle);

    for (int i = 0; i < cycle; i++)
    {
    beginOffset = i * n;
    endOffset = beginOffset + n;

    v2D.reserve(n);

    copy(v1.begin() + beginOffset, v1.begin() + endOffset,
    v2D.begin());
    }

    Basically what I am doing is taking n numbers at a time iteratively,
    and push them to a 2D vector of size 2-by-n. I also tried using
    back_inserter in place of v2D.begin() but it didn't help. Am I
    doing something wrong? Thanks.

    Jessica
     
    Jessica, Jun 25, 2003
    #1
    1. Advertising

  2. "Jessica" <> wrote...
    > Hi,
    > I do not have a lot of experience with STL and I hope some of you
    > might be able to help me on this seemingly elementary question.
    >
    > I have a vector of doubles (v1). I am trying to copy the values to
    > a 2D vector, of which every vector has the same length. I tried the
    > following but I get a "System.NullReferenceException" error when I ran
    > it.
    >
    >
    > cycle = 2;
    > n = 12;
    >
    > vector< vector<double> >v2D;
    > v2D.reserve(cycle);


    'reserve' doesn't create elements in that vector. It just
    assures that a certain number of 'push_back's won't cause
    reallocation.

    >
    > for (int i = 0; i < cycle; i++)
    > {
    > beginOffset = i * n;
    > endOffset = beginOffset + n;
    >
    > v2D.reserve(n);


    Again, 'reserve' doesn't create elements. You need 'resize'.

    >
    > copy(v1.begin() + beginOffset, v1.begin() + endOffset,
    > v2D.begin());
    > }
    >
    > Basically what I am doing is taking n numbers at a time iteratively,
    > and push them to a 2D vector of size 2-by-n. I also tried using
    > back_inserter in place of v2D.begin() but it didn't help. Am I
    > doing something wrong? Thanks.


    Yes. You're using 'reserve' instead of 'resize'.

    Victor
     
    Victor Bazarov, Jun 25, 2003
    #2
    1. Advertising

  3. Jessica

    Andre Kostur Guest

    (Jessica) wrote in
    news::

    > Hi,
    > I do not have a lot of experience with STL and I hope some of you
    > might be able to help me on this seemingly elementary question.
    >
    > I have a vector of doubles (v1). I am trying to copy the values to
    > a 2D vector, of which every vector has the same length. I tried the
    > following but I get a "System.NullReferenceException" error when I ran
    > it.
    >
    >
    > cycle = 2;
    > n = 12;
    >
    > vector< vector<double> >v2D;
    > v2D.reserve(cycle);
    >
    > for (int i = 0; i < cycle; i++)
    > {
    > beginOffset = i * n;
    > endOffset = beginOffset + n;
    >
    > v2D.reserve(n);
    >
    > copy(v1.begin() + beginOffset, v1.begin() + endOffset,
    > v2D.begin());
    > }
    >
    > Basically what I am doing is taking n numbers at a time iteratively,
    > and push them to a 2D vector of size 2-by-n. I also tried using
    > back_inserter in place of v2D.begin() but it didn't help. Am I
    > doing something wrong? Thanks.
    >
    > Jessica


    Reserve() doesn't do what you think it does:

    ..reserve() makes the internal representation of the vector big enough to
    hold the specified number of objects, but does not actually construct
    them. Thus .capacity() of the vector will be >= the specified size, but
    ..size() will still be 0.

    ..resize() will make the vector big enough the hold the specified number
    of objects, and will construct that number of objects. Thus .capacity()
    of the vector will be >= the specified size, and .size() will be the
    specified size (each object will be default constructed).

    In your code, you v2D.reserve(2), and then try to access those
    (unconstructed) objects. You also try to v2D[0].reserve(12), and v2D
    [1].reserve(12), and try to access those as well. Your back_inserter
    idea would have worked, but your original v2D.reserve(2) will have
    already fouled things up. If you replace your calls to .reserve() with
    calls to .resize() you should be fine (no back_inserter).
     
    Andre Kostur, Jun 25, 2003
    #3
  4. Jessica

    Jessica Guest

    Ah. Thanks guys. That explains the strange exceptions that I've been
    getting. So when should reserve() be used? I guess what confused me
    is this paragraph that I read from SGI:

    "Reserve() causes a reallocation manually. The main reason for using
    reserve() is efficiency: if you know the capacity to which your vector
    must eventually grow, then it is usually more efficient to allocate
    that memory all at once rather than relying on the automatic
    reallocation scheme. The other reason for using reserve() is so that
    you can control the invalidation of iterators."

    Thank you again!

    Jessica


    Andre Kostur <> wrote in message news:<Xns93A5A13527BFnntpspamkosturnet@209.53.75.21>...
    > (Jessica) wrote in
    > news::
    >
    > > Hi,
    > > I do not have a lot of experience with STL and I hope some of you
    > > might be able to help me on this seemingly elementary question.
    > >
    > > I have a vector of doubles (v1). I am trying to copy the values to
    > > a 2D vector, of which every vector has the same length. I tried the
    > > following but I get a "System.NullReferenceException" error when I ran
    > > it.
    > >
    > >
    > > cycle = 2;
    > > n = 12;
    > >
    > > vector< vector<double> >v2D;
    > > v2D.reserve(cycle);
    > >
    > > for (int i = 0; i < cycle; i++)
    > > {
    > > beginOffset = i * n;
    > > endOffset = beginOffset + n;
    > >
    > > v2D.reserve(n);
    > >
    > > copy(v1.begin() + beginOffset, v1.begin() + endOffset,
    > > v2D.begin());
    > > }
    > >
    > > Basically what I am doing is taking n numbers at a time iteratively,
    > > and push them to a 2D vector of size 2-by-n. I also tried using
    > > back_inserter in place of v2D.begin() but it didn't help. Am I
    > > doing something wrong? Thanks.
    > >
    > > Jessica

    >
    > Reserve() doesn't do what you think it does:
    >
    > .reserve() makes the internal representation of the vector big enough to
    > hold the specified number of objects, but does not actually construct
    > them. Thus .capacity() of the vector will be >= the specified size, but
    > .size() will still be 0.
    >
    > .resize() will make the vector big enough the hold the specified number
    > of objects, and will construct that number of objects. Thus .capacity()
    > of the vector will be >= the specified size, and .size() will be the
    > specified size (each object will be default constructed).
    >
    > In your code, you v2D.reserve(2), and then try to access those
    > (unconstructed) objects. You also try to v2D[0].reserve(12), and v2D
    > [1].reserve(12), and try to access those as well. Your back_inserter
    > idea would have worked, but your original v2D.reserve(2) will have
    > already fouled things up. If you replace your calls to .reserve() with
    > calls to .resize() you should be fine (no back_inserter).
     
    Jessica, Jun 26, 2003
    #4
  5. Jessica

    Andre Kostur Guest

    (Jessica) wrote in
    news::

    > Ah. Thanks guys. That explains the strange exceptions that I've been
    > getting. So when should reserve() be used? I guess what confused me
    > is this paragraph that I read from SGI:
    >
    > "Reserve() causes a reallocation manually. The main reason for using
    > reserve() is efficiency: if you know the capacity to which your vector
    > must eventually grow, then it is usually more efficient to allocate
    > that memory all at once rather than relying on the automatic
    > reallocation scheme. The other reason for using reserve() is so that
    > you can control the invalidation of iterators."
    >
    > Thank you again!


    No problem :)

    Reserve is good if you know ahead of time that you are going to be doing
    a bunch of push_back() (or using back_inserter) on your vector. Recall
    that vector will automatically resize to fit the number of objects.

    Let's assume that you start with an empty vector, and you start pushing
    on 10000 objects. (Note that the reallocation scheme I'm going to use
    isn't necessarily the one your vector uses, but it uses something along
    the same idea).

    The vector assumes that you are going to push some items on it, so it
    preallocated space for 16 items.

    Your first 16 push_backs simply copy your items into those 16 spaces.

    The 17th push_back causes a reallocation. Space for 32 objects is
    created, and the first 16 objects are copied into this new space. The
    old space is deallocated, and then the new object is copied into the 17th
    space.

    The next 15 push_backs simply copy the items into the 18th through 32nd
    space.

    The 33rd push_back causes a reallocation. Space for 64 objects is
    created, the first 32 objects are copied, etc....

    At the 65th push_back this happens again (space for 128)

    Again at 129, 257, 513, 1025, 2049, 4097, and 8193. Finally you have a
    vector containing 10000 objects, and has space for 16k objects.

    Constrast this with calling reserve first.

    You start by doing a .reserve(10000) on the vector. You then start doing
    your push_backs. All 10000 will be copied into the vector. No more
    reallocations, and no "extra" copies of objects being made. (Recall that
    in the first scenario, the first object that you push_back()ed into the
    vector is copied an extra 10 times as it's moved from each allocation to
    the next new allocation).


    So by calling reserve first, you save 10 extra dynamic memory
    allocations, and thousands of extra copies of objects.
     
    Andre Kostur, Jun 26, 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. Dennis
    Replies:
    1
    Views:
    2,585
    Dennis
    Jun 7, 2004
  2. CD
    Replies:
    2
    Views:
    806
    Victor Bazarov
    Oct 5, 2004
  3. Replies:
    2
    Views:
    417
  4. Replies:
    8
    Views:
    1,925
    Csaba
    Feb 18, 2006
  5. Luca Risolia

    STL map to STL vector

    Luca Risolia, Jan 13, 2014, in forum: C++
    Replies:
    32
    Views:
    372
    Seungbeom Kim
    Jan 18, 2014
Loading...

Share This Page