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

Home Posts Topics Members FAQ

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_typ e 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 2313
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_typ e 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.co m> <http://www.dietmar-kuehl.de/>
<http://www.contendix.c om> - 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_typ e 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
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.
9
3014
by: Alex Vinokur | last post by:
--------- #include <vector> using namespace std; struct Foo { Foo (int) {} }; int main () { vector<Foo> v1;
1
1282
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 pile.h) Can someone tell why it says that? Also, I don't understand why I am not allowed to put const on this method: void printLn(); when I write void printLn() const; It says : 'printLn' : overloaded member
8
5111
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to copy the capacity and reserve methods, because I just need a growable array. I get to considering...
7
3009
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 501st element is going to cause some more allocation -- for arguments sake if the vector re-grows to accomodate 1000 elements, I play around with it and completely erase everything in it after I am done. Now how much does the vector hold? Do I...
3
2472
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 construction and destruction of a temporary, and might still leave some excess capacity in the vector.
17
11926
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 storage. v.reserve(300); ///at max I have 300 data. now, my_data doesn't have a default ctor (as it doesn't have a default state).
32
4024
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 large amount of data, that I read off using an ifstream object, I don't want to use the push_back method because this grows the vector<vector<double>dynamically, and that will kill my execution time. So, I want to reserve space first, using, of...
23
3477
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 4.1.2-12) The program below fails, but if the reserve(en)
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
9552
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...
1
6796
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4607
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4877
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3315
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 we have to send another system
2
2787
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2215
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.