473,471 Members | 1,728 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

vector reserve() and clear()

vector<stringbuf_string;
buf_string.reserve(256);
vector<intbuf_mat_prices;
buf_mat_prices.reserve(1000);

During loops I fill the vectors and then I empty them with commands
like buf_string.clear();
buf_mat_prices.clear();

Does this mean that the memory allocation returns to default or is my
original reserve still in place?

Nov 2 '06 #1
3 4819

pkirk25 wrote in message
<11**********************@f16g2000cwb.googlegroups .com>...
vector<stringbuf_string;
buf_string.reserve(256);
vector<intbuf_mat_prices;
buf_mat_prices.reserve(1000);

During loops I fill the vectors and then I empty them with commands
like buf_string.clear();
buf_mat_prices.clear();

Does this mean that the memory allocation returns to default or is my
original reserve still in place?

// Prove it for yourself: // using std::cout;
cout << buf_string.size() << std::endl; // S/B '0'
cout << buf_string.capacity() << std::endl; // clear() doesn't resize

cout << buf_mat_prices.size() << std::endl; // S/B '0'
cout << buf_mat_prices.capacity() << std::endl; // clear() doesn't
resize

You can re-size the vector by using (surprise!):

buf_string.resize(25);
cout << buf_string.size() << std::endl;
cout << buf_string.capacity() << std::endl;

That help?
--
Bob R
POVrookie
Nov 2 '06 #2
Very neat - many thanks! I didn't see the capacity member :-(

Nov 2 '06 #3
pkirk25 wrote:
vector<intbuf_mat_prices;
buf_mat_prices.reserve(1000);
buf_mat_prices.clear();

Does this mean that the memory allocation returns to default or is my
original reserve still in place?
No, you still have the reservation. To "unreserve" you need to do a
swap-with-new:

vector<int>( ).swap(buf_mat_prices).

The vector<int>( ) is a temporary that gets the 1000 reservation, and
then dies.
buf_mat_prices gets the capacity of the freshly constructed temporary.

HTH,
Michiel Salters

Nov 6 '06 #4

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...
10
by: Huibuh | last post by:
The following code is a function called from the main. Sence is to initialize a vector of the dimension of "a". "b" is the according value (all the same in a vector). The problem is that the...
10
by: Bob | last post by:
Here's what I have: void miniVector<T>::insertOrder(miniVector<T>& v,const T& item) { int i, j; T target; vSize += 1; T newVector; newVector=new T;
8
by: Alex Vinokur | last post by:
What is relation between std::vector's reserve() and erase()/clear()? vector<int> v; v.reserve(100); v.resize(100); v.erase(v.end()); How many elements are reserved here: 100 or 99?
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...
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...
13
by: smp | last post by:
Does anyone know why making a vector of pointers is so much less efficient than a vector of objects? For a simple example: int num = 20; vector<int*v_int_ptr; v_int_ptr.reserve(num); ...
3
by: clinisbut | last post by:
I'm trying to understand what's wrong with this: void getAllData( std::vector<unsigned char&char_array ) { char_array.clear(); char_array.reserve( 11 ); char_array = 'A'; //<----No...
0
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,...
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
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
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
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,...
1
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.