vector reserve() and clear()

Discussion in 'C++' started by pkirk25, Nov 2, 2006.

  1. pkirk25

    pkirk25 Guest

    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?
    pkirk25, Nov 2, 2006
    #1
    1. Advertising

  2. pkirk25

    BobR Guest

    pkirk25 wrote in message
    <>...
    > 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
    BobR, Nov 2, 2006
    #2
    1. Advertising

  3. pkirk25

    pkirk25 Guest

    Very neat - many thanks! I didn't see the capacity member :-(
    pkirk25, Nov 2, 2006
    #3
  4. pkirk25

    Guest

    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
    , Nov 6, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. john smith

    vector.reserve

    john smith, Jul 25, 2003, in forum: C++
    Replies:
    5
    Views:
    688
    John Harrison
    Jul 25, 2003
  2. Alex Vinokur
    Replies:
    8
    Views:
    395
    Alex Vinokur
    Sep 23, 2005
  3. Replies:
    8
    Views:
    1,911
    Csaba
    Feb 18, 2006
  4. Jess
    Replies:
    9
    Views:
    407
  5. mathieu
    Replies:
    12
    Views:
    1,230
    Jeff Flinn
    May 25, 2011
Loading...

Share This Page