STL vector question

J

Jessica

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
 
V

Victor Bazarov

Jessica said:
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
 
A

Andre Kostur

(e-mail address removed) (Jessica) wrote in
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).
 
J

Jessica

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 said:
(e-mail address removed) (Jessica) wrote in
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).
 
A

Andre Kostur

(e-mail address removed) (Jessica) wrote in
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top