473,386 Members | 1,799 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,386 software developers and data experts.

vector: capacity and resize

There are a few things I do not understand about the vector class
in de standard C++ library. The description of the resize member
function says:
void
resize(size_type sz);Alters the size of self. If the new size (sz) is greater than the current size, then sz-size() instances of the default value of type T are inserted at the end of the vector. If the new size is smaller than the current capacity, then the vector is truncated by erasing size()-sz elements off the end. If sz is equal to capacity then no action is taken.

Why does it compare the new size with both the current size and with the capacity?
I understood that the size and the capacity are different properties. If so, then this
description seems inconsistent to me.
Does this description mean that the capacity of a vector can be decreased by resize?
I understood that the capacity was never decreased in order to reduce the overhead
of allocating/deallocating dynamic memory.

Can somebody enlighten me?

F.Z.

Jul 23 '05 #1
2 2293
Fred Zwarts wrote:
There are a few things I do not understand about the vector class
in de standard C++ library. The description of the resize member
function says:
void
resize(size_type sz);Alters the size of self. If the new size (sz) is

greater than the current size, then sz-size() instances of the default
value of type T are inserted at the end of the vector. If the new size
is smaller than the current capacity, then the vector is truncated by
erasing size()-sz elements off the end. If sz is equal to capacity then
no action is taken.

The above description is misleading: I think the "capacity" it speaks
of is actually the vector's size. The description unfortunately uses
a term also used for the size of containers (capacity) although this
term can easily be confused with an entirely different concept in the
vector class. The standard description of 'resize()' effects is rather
clear and does not use 'capacity()'.
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #2
Fred Zwarts wrote:
There are a few things I do not understand about the vector class
in de standard C++ library. The description of the resize member
function says:
void
resize(size_type sz);Alters the size of self. If the new size (sz) is greater than the current size, then sz-size() instances of the default value of type T are inserted at the end of the vector. If the new size is smaller than the current capacity, then the vector is truncated by erasing size()-sz elements off the end. If sz is equal to capacity then no action is taken.

Why does it compare the new size with both the current size and with the capacity?
I understood that the size and the capacity are different properties. If so, then this
description seems inconsistent to me.
Does this description mean that the capacity of a vector can be decreased by resize?
I understood that the capacity was never decreased in order to reduce the overhead
of allocating/deallocating dynamic memory.

Can somebody enlighten me?


It's been rewritten in the current working draft. It now reads

Effects:
if ( sz > size ())
insert (end () , sz - size () , c);
else if ( sz < size ())
erase ( begin ()+sz , end ());
else
; // do nothing

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3

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

Similar topics

10
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...
9
by: Alex Vinokur | last post by:
--------- #include <vector> using namespace std; struct Foo { Foo (int) {} }; int main () { vector<Foo> v1;
1
by: =steFF= | last post by:
Hi, I am trying to print the values I have in a vector and my compiler says: error C2227: left of '->printLn' must point to class/struct/union on this instruction: m_data->printLn(); (in my file...
8
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...
7
by: Dilip | last post by:
If you reserve a certain amount of memory for a std::vector, what happens when a reallocation is necessary because I overshot the limit? I mean, say I reserve for 500 elements, the insertion of...
3
by: leeps_my | last post by:
I'm thinking of using "resize-to-size" to do the trimming: aVector.resize( aVector.size( ) ); I'm wondering why Scott Meyers was recommending "swap trick" instead, since the trick involves...
17
by: toton | last post by:
Hi, I am using a vector to reserve certain amount of memory, and reuse it for new set of data, to avoid reallocation of memory. so the call is something like vector<my_datav;///the temporary...
32
by: T. Crane | last post by:
Hi, I'm struggling with how to initialize a vector<vector<double>> object. I'm pulling data out of a file and storing it in the vector<vector<double>object. Because any given file will have a...
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.