473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL vector question

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.NullRef erenceException " error when I ran
it.
cycle = 2;
n = 12;

vector< vector<double> >v2D;
v2D.reserve(cyc le);

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
Jul 19 '05 #1
4 11432
"Jessica" <jl*****@earthl ink.net> 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.NullRef erenceException " error when I ran
it.
cycle = 2;
n = 12;

vector< vector<double> >v2D;
v2D.reserve(cyc le);
'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
Jul 19 '05 #2
jl*****@earthli nk.net (Jessica) wrote in
news:d1******** *************** ***@posting.goo gle.com:
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.NullRef erenceException " error when I ran
it.
cycle = 2;
n = 12;

vector< vector<double> >v2D;
v2D.reserve(cyc le);

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).
Jul 19 '05 #3
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 <nn******@kostu r.net> wrote in message news:<Xn******* *************** ********@209.53 .75.21>...
jl*****@earthli nk.net (Jessica) wrote in
news:d1******** *************** ***@posting.goo gle.com:
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.NullRef erenceException " error when I ran
it.
cycle = 2;
n = 12;

vector< vector<double> >v2D;
v2D.reserve(cyc le);

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).

Jul 19 '05 #4
jl*****@earthli nk.net (Jessica) wrote in
news:d1******** *************** ***@posting.goo gle.com:
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.
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
7077
by: Stefan Höhne | last post by:
Hi, as I recon, std::vector::clear()'s semantics changed from MS VC++ 6.0 to MS' DOT.NET - compiler. In the 6.0 version the capacity() of the vector did not change with the call to clear(), in DOT.NET the capacity() is reduced to 0.
12
3159
by: No Such Luck | last post by:
Hi All: I'm not sure if this is the right place to ask this question, but I couldn't find a more appropriate group. This is more of a theory question regarding an algorithm implemented in C, not necessarily a C language question. I'm trying to break up a vector into an arbitrary number of subvectors, equal (or as near to equal) in size as possible. My problem occurs when the vector is not evenly divisible by the number of subvectors...
24
2954
by: toton | last post by:
Hi, I want to have a vector like class with some additional functionality (cosmetic one). So can I inherit a vector class to add the addition function like, CorresVector : public vector<Corres>{ public: void addCorres(Corres& c); //it do little more than push_back function. }
2
3321
by: danielhdez14142 | last post by:
Some time ago, I had a segment of code like vector<vector<int example; f(example); and inside f, I defined vector<int>'s and used push_back to get them inside example. I got a segmentation fault which I resolved by doing vector<vector<int example; example.push_back(vector<int>());
9
3759
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
7
3894
by: nw | last post by:
Hi, We've been having a discussion at work and I'm wondering if anyone here would care to offer an opinion or alternative solution. Aparently in the C programming HPC community it is common to allocate multidimentional arrays like so: int *v_base = (int *) malloc(1000000*sizeof(int)); int **v = (int **) malloc(1000*sizeof(int *));
6
11616
by: jmsanchezdiaz | last post by:
CPP question: if i had a struct like "struct str { int a; int b };" and a vector "std::vector < str test;" and wanted to push_back a struct, would i have to define the struct, fill it, and then push_back it, or could i pushback the two ints directly somehow? Thanks for all.
13
1924
by: prasadmpatil | last post by:
I am new STL programming. I have a query regarding vectors. If I am iterating over a vector using a iterator, but do some operations that modify the size of the vector. Will the iterator recognize this? I wrote the following program to test this out. #include <fstream> #include <iostream> #include <string>
6
7381
by: Mr. K.V.B.L. | last post by:
I want to start a map with keys but an empty vector<string>. Not sure what the syntax is here. Something like: map<string, vector<string MapVector; MapVector.insert(make_pair("string1", new vector<string>)); MapVector.insert(make_pair("string2", new vector<string>)); MapVector.insert(make_pair("string3", new vector<string>));
0
8991
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8830
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9544
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9324
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9247
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8243
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2783
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.