Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > vector reserve() and clear()

Reply
Thread Tools

vector reserve() and clear()

 
 
pkirk25
Guest
Posts: n/a
 
      11-02-2006
vector<string> buf_string;
buf_string.reserve(256);
vector<int> buf_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?

 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      11-02-2006

pkirk25 wrote in message
< .com>...
> vector<string> buf_string;
> buf_string.reserve(256);
> vector<int> buf_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


 
Reply With Quote
 
 
 
 
pkirk25
Guest
Posts: n/a
 
      11-02-2006
Very neat - many thanks! I didn't see the capacity member

 
Reply With Quote
 
Michiel.Salters@tomtom.com
Guest
Posts: n/a
 
      11-06-2006
pkirk25 wrote:
> vector<int> buf_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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM
STL algorithm problem vector<vector<double> > and find Dennis C++ 1 06-07-2004 10:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57