473,396 Members | 2,082 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

default size of STL vector question

hi, all
What is the default size of a stl vector? Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)? Of course I need to resize the
vector after it is populated. Thanks for help.
zl2k

Oct 14 '07 #1
7 7283
zl2k wrote:
hi, all
What is the default size of a stl vector?
There isn't one.
Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)?
Probably.

--
Ian Collins.
Oct 14 '07 #2
Ian Collins a écrit :
zl2k wrote:
>hi, all
What is the default size of a stl vector?

There isn't one.
Create a vector an use vector<>::capacity() to know your STL
implementation's default size.
>
>Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)?

Probably.
You can use vector<>::reserve() instead. That way push_back() won't
cause reallocation and you can keep your vector size consistant with the
size of data held (and it doesn't initialize/destroy unnecessary data,
not that it matters with ints).

Michael
Oct 14 '07 #3
On Oct 14, 12:19 am, Michael DOUBEZ <michael.dou...@free.frwrote:
Ian Collins a écrit :
zl2k wrote:
hi, all
What is the default size of a stl vector?
There isn't one.

Create a vector an use vector<>::capacity() to know your STL
implementation's default size.
Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)?
Probably.

You can use vector<>::reserve() instead. That way push_back() won't
cause reallocation and you can keep your vector size consistant with the
size of data held (and it doesn't initialize/destroy unnecessary data,
not that it matters with ints).

Michael
But if I use reserve(maxAmount), then the maxAmount of memory is
acturally occupied by the vector, no matter how many elements are
actually there. Is that right?

Oct 14 '07 #4
zl2k a écrit :
On Oct 14, 12:19 am, Michael DOUBEZ <michael.dou...@free.frwrote:
>Ian Collins a écrit :
>>zl2k wrote:
Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)?
Probably.
You can use vector<>::reserve() instead. That way push_back() won't
cause reallocation and you can keep your vector size consistant with the
size of data held (and it doesn't initialize/destroy unnecessary data,
not that it matters with ints).
But if I use reserve(maxAmount), then the maxAmount of memory is
acturally occupied by the vector, no matter how many elements are
actually there. Is that right?
Yes but the result is the same with resize(). The standard does not
require that resize() give back memory and I assume that in most
implementation it doesn't.
It is one item of Herb Sutter's "Effective C++". There is a way that
potentialy allows you to get back the memory:
vector<intmyVector;//lot of memory reserved relatively to actual size

{vector<inttmp(myVector);
myVector.swap(tmp);
}

//here there is a chance to have myVector.capacity() reduced

Another possibility is that vector isn't really the container you need
or you can use another container to build the data and then copy them in
a vector.

Michael
Oct 14 '07 #5
On Oct 14, 5:25 am, zl2k <kdsfin...@gmail.comwrote:
What is the default size of a stl vector?
0. Unless you specify a size, a vector is created empty,
without any elements.
Suppose I have integers needs to store in a vector and I know
the max number of integer is, say, 1000. Will it be more
efficient that I first allocate size of 1000 and then use
{vector[counter] = aNumber; counter++} to populate the vector
than by push_back(aNumber)?
It really depends on the implementation, but if you just do
push_back, without any reserve, then it's almost sure that using
push_back will be slower. On the other hand, the difference
between:

std::vector< int a( 1000 ) ;
for ( int i = 0 ; i < 1000 ; ++ i ) {
a[ i ] = ...
}

and

std::vector< int a ;
a.reserve( 1000 ) ;
for ( int i = 0 ; i < 1000 ; ++ i ) {
a.push_back( ... ) ;
}

will probably vary between implementations, and even between
processors, using the same implementation.

If instead of int, the vector is of a user defined type with an
expensive constructor and/or expensive assignment, the push_back
will be more favorable.
Of course I need to resize the
vector after it is populated.
Resizing a vector to make it smaller is very, very fast for the
built-in types. If a user defined type has a non-trivial
destructor, however, that destructor will be called for each
element removed by the resize.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 14 '07 #6
"zl2k" <kd*******@gmail.comwrote in message
news:11*********************@z24g2000prh.googlegro ups.com...
What is the default size of a stl vector?
Zero.
Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)?
Your best bet is probably to reserve enough space (that is, call reserve
rather than resize) and then call push_back repeatedly.

Example:

vector<intv;
v.reserve(n);
for (int i = 0; i < n; ++i)
v.push_back(i);

That way, you avoid having to reallocate vector elements, and you also avoid
having to construct elements that you overwrite later.
Oct 14 '07 #7
On Oct 14, 6:00 pm, "Andrew Koenig" <a...@acm.orgwrote:
"zl2k" <kdsfin...@gmail.comwrote in message
news:11*********************@z24g2000prh.googlegro ups.com...
What is the default size of a stl vector?
Zero.
Suppose I have integers
needs to store in a vector and I know the max number of integer is,
say, 1000. Will it be more efficient that I first allocate size of
1000 and then use {vector[counter] = aNumber; counter++} to populate
the vector than by push_back(aNumber)?
Your best bet is probably to reserve enough space (that is, call reserve
rather than resize) and then call push_back repeatedly.
Example:
vector<intv;
v.reserve(n);
for (int i = 0; i < n; ++i)
v.push_back(i);
That way, you avoid having to reallocate vector elements, and
you also avoid having to construct elements that you overwrite
later.
That was more or less what I'd assumed until I actually measured
it. For an std::vector<int>, at least for the implementation I
measured (g++ on Sun Sparc, IIRC), doing the initializations was
slightly faster than the push_back's.

This obviously depends on the type--if default construction
followed by assignment is significantly slower than for int, for
example---and the implementation, and as a general rule, I'd
still go with reserve() plus the push_back(), since it will
always be almost as fast, and protects against the case where
construction plus assignment is expensive. But if performance
matters, you have to profile.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Oct 14 '07 #8

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

Similar topics

10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
1
by: Kristoffer Vinther | last post by:
Hi, I need to store of parts of containers. The interface to the store could look something like this (the code in this post serves as illustrations, it is not meant to be 100% correct): ...
27
by: Marcus Kwok | last post by:
I am getting warnings when comparing a (regular) int to the value returned from std::vector.size() in code similar to the following: int i = //stuff if (i >= vec.size()) The compiler gives...
6
by: mati-006 | last post by:
Hi Short question for which I can't find answer: I want to initialize the array of objects with the same values. new MyType; calls default constructor new MyType(val1, val2); initializes only...
7
by: ottawajn | last post by:
Dear there, I want to do the follows. (1)initial an array, say myarray={1 2 3.1 4}; (2)insert zeros in myarray, like {1,0,2,0,3.1,0,4,0}; (3)let myarray={1,0,2,0,3.1,0,4,0}; (I have not...
43
by: JohnQ | last post by:
Are a default constructor, destructor, copy constructor and assignment operator generated by the compiler for a struct if they are not explicitely defined? I think the answer is yes, because...
8
by: t | last post by:
The stack container adaptor uses deque as its default underlying sequential container. I don't understand why since we only add elements and remove elements from one side of a stack. Why isn't...
2
by: campos | last post by:
Hi all, I want to know the default capacity when creating STL vector and list. Any doc or manual I can refer to? Thanks in advance!
4
by: shuisheng | last post by:
Dear All, I have a question. Assume #include <vector> using namespace std; class A { private:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
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
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...
0
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
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,...

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.