Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > STL vector question

Reply
Thread Tools

STL vector question

 
 
Jessica
Guest
Posts: n/a
 
      06-25-2003
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[i].reserve(n);

copy(v1.begin() + beginOffset, v1.begin() + endOffset,
v2D[i].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[i].begin() but it didn't help. Am I
doing something wrong? Thanks.

Jessica
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-25-2003
"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[i].reserve(n);


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

>
> copy(v1.begin() + beginOffset, v1.begin() + endOffset,
> v2D[i].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[i].begin() but it didn't help. Am I
> doing something wrong? Thanks.


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

Victor


 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      06-25-2003
(Jessica) wrote in
news: om:

> 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[i].reserve(n);
>
> copy(v1.begin() + beginOffset, v1.begin() + endOffset,
> v2D[i].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[i].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).
 
Reply With Quote
 
Jessica
Guest
Posts: n/a
 
      06-26-2003
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.2 1>...
> (Jessica) wrote in
> news: om:
>
> > 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[i].reserve(n);
> >
> > copy(v1.begin() + beginOffset, v1.begin() + endOffset,
> > v2D[i].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[i].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).

 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      06-26-2003
(Jessica) wrote in
news: om:

> 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 12

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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem storing tvmet vector objects in an stl vector container alexjcollins@gmail.com C++ 2 09-08-2008 09:18 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
Vector of STL maps versus Vector of objects amolpan@gmail.com C++ 2 07-26-2005 10:16 PM
Automatic Conversion of STL Containers: e.g. from vector<derived*> to vector<base*> CD C++ 2 10-05-2004 02:29 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57