473,508 Members | 2,229 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to reintialize a vector

hi, there
Suppose I have declared a vector in the header file

vector<int> v1;
vector< vector<int> > v2;

Now I need to reinitialize the vector somewhere later to different
sizes. How can I do that for v2? For v1, I can always say
v1.assign(10,0);
to set its size to 10 and populate with 0s.

For v2, can I reinitialize it with known size of one dimension (row)
and with unknown size of another dimension (collumn)? How? (Or maybe I
can always declare the unknow size to 1 since the vector can grow?)

Thanks ahead.

zl2k

May 16 '06 #1
2 2799
zl2k wrote:
hi, there
Suppose I have declared a vector in the header file

vector<int> v1;
vector< vector<int> > v2;

Now I need to reinitialize the vector somewhere later to different
sizes. How can I do that for v2? For v1, I can always say
v1.assign(10,0);
to set its size to 10 and populate with 0s.

For v2, can I reinitialize it with known size of one dimension (row)
and with unknown size of another dimension (collumn)? How? (Or maybe I
can always declare the unknow size to 1 since the vector can grow?)

Thanks ahead.

zl2k


You can do it in a loop:

v2.resize( newRows );
for( unsigned n=0; n < v2.size(); ++n )
{
v2[ n ].resize( newCols );
}

Or you could do it less efficiently but more concisely:

v2.swap( vector< vector<int> >( newRows, vector<int>( newCols ) );

Cheers! --M

May 16 '06 #2
mlimber wrote:
Or you could do it less efficiently but more concisely:

v2.swap( vector< vector<int> >( newRows, vector<int>( newCols ) );


....add an extra closing parenthesis in there.

M

May 16 '06 #3

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

Similar topics

9
3194
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
9
2955
by: luigi | last post by:
Hi, I am trying to speed up the perfomance of stl vector by allocating/deallocating blocks of memory manually. one version of the code crashes when I try to free the memory. The other version...
7
10613
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
8
5094
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
0
7129
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...
0
7333
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,...
0
7398
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...
1
7061
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...
0
7502
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...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
428
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.