std::vector's reserve(), erase() and clear()

Discussion in 'C++' started by Alex Vinokur, Sep 23, 2005.

  1. Alex Vinokur

    Alex Vinokur Guest

    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?

    v.reserve(200);
    v.resize();
    v.clear();

    How many elements are reserved here: 200 or 0?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Sep 23, 2005
    #1
    1. Advertising

  2. Alex Vinokur

    Guest

    Alex Vinokur 写é“:

    > 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?
    >
    > v.reserve(200);
    > v.resize();
    > v.clear();
    >
    > How many elements are reserved here: 200 or 0?
    >
    >
    > --
    > Alex Vinokur
    > email: alex DOT vinokur AT gmail DOT com
    > http://mathforum.org/library/view/10978.html
    > http://sourceforge.net/users/alexvn


    you can test it on you computer,it's easy!
     
    , Sep 23, 2005
    #2
    1. Advertising

  3. Alex Vinokur

    Mike Wahler Guest

    "Alex Vinokur" <> wrote in message
    news:dgvvuj$mh2$...
    > What is relation between std::vector's reserve() and erase()/clear()?
    >
    > vector<int> v;
    >
    > v.reserve(100);
    > v.resize(100);
    > v.erase(v.end());


    This last line will produce undefined behavior. 'end()'
    points to one element past the last element in
    the vector. You can't 'erase' it.

    >
    > How many elements are reserved here: 100 or 99?


    Exactly the number you used: 100
    Then undefined behavior happens.

    >
    > v.reserve(200);
    > v.resize();


    This is an error. At least one argument (the new size)
    is required.

    > v.clear();
    >
    > How many elements are reserved here: 200 or 0?


    Exactly the number you used: 200
    But the call to resize is in error.

    -Mike
     
    Mike Wahler, Sep 23, 2005
    #3
  4. Alex Vinokur

    Alex Vinokur Guest

    <> wrote in message news:...

    Alex Vinokur ??:

    > What is relation between std::vector's reserve() and erase()/clear()?

    [snip]
    you can test it on you computer,it's easy!


    ====== foo.cpp ======
    #include <vector>
    #include <iostream>
    using namespace std;

    #define SHOW cout << "capacity = " << v.capacity() << "\t size = " << v.size() << endl;

    int main()
    {
    vector<int> v;
    SHOW;
    v.reserve(100);
    SHOW;
    v.resize(100);
    SHOW;
    v.erase(v.end() - 1);
    SHOW;
    v.clear();
    SHOW;

    return 0;
    }
    =====================



    1. Output for GNU g++ 3.4.4, GNU gpp 4.0.1, Bolrand C++ 5.5.1, Digital Mars 8.38n
    ---------------------------
    capacity = 0 size = 0
    capacity = 100 size = 0
    capacity = 100 size = 100
    capacity = 100 size = 99
    capacity = 100 size = 0
    ---------------------------


    2. Output for Microsoft C++ 13.00.9466
    ---------------------------
    capacity = 0 size = 0
    capacity = 100 size = 0
    capacity = 100 size = 100
    capacity = 100 size = 99
    capacity = 0 size = 0
    ---------------------------


    We can see that capacity == 0 after clear() for Microsoft C++.
    Is it correct behavior?


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Sep 23, 2005
    #4
  5. Alex Vinokur

    Alex Vinokur Guest

    "Mike Wahler" <> wrote in message news:APMYe.2466$...
    >
    > "Alex Vinokur" <> wrote in message

    [snip]
    > > v.erase(v.end());

    >
    > This last line will produce undefined behavior. 'end()'
    > points to one element past the last element in
    > the vector. You can't 'erase' it.


    Thanks. Updated in my new message at
    http://groups.google.com/group/comp.lang.c /msg/04ad33960f339af5

    >
    > >
    > > How many elements are reserved here: 100 or 99?

    >
    > Exactly the number you used: 100
    > Then undefined behavior happens.
    >
    > >
    > > v.reserve(200);
    > > v.resize();

    >
    > This is an error.

    Of course, my mistake.
    > At least one argument (the new size)
    > is required.
    >
    > > v.clear();
    > >
    > > How many elements are reserved here: 200 or 0?

    >
    > Exactly the number you used: 200

    See my new message at
    http://groups.google.com/group/comp.lang.c /msg/04ad33960f339af5
    > But the call to resize is in error.
    >
    > -Mike
    >
    >


    --
    Alex Vinokur
    email: alex DOT vinokur AT gmail DOT com
    http://mathforum.org/library/view/10978.html
    http://sourceforge.net/users/alexvn
     
    Alex Vinokur, Sep 23, 2005
    #5
  6. Alex Vinokur

    persenaama Guest

    > How many elements are reserved here: 100 or 99?

    v.capacity() could tell you if you fix the erase().

    > How many elements are reserved here: 200 or 0?


    v.capacity() could tell you if you fix the resize().
     
    persenaama, Sep 23, 2005
    #6
  7. Alex Vinokur

    msalters Guest

    schreef:

    > Alex Vinokur 写é“:
    >
    > > What is relation between std::vector's reserve() and erase()/clear()?

    >
    > you can test it on you computer,it's easy!


    Not really. I.e. If I call reserve(100), and get capacity()==128,
    that's OK. However, you might get capacity()==100 and that's OK too.
    You can't induce the C++ standard.

    HTH,
    Michiel Salters
     
    msalters, Sep 23, 2005
    #7
  8. "Alex Vinokur" <> wrote in message
    news:dgvvuj$mh2$...

    > What is relation between std::vector's reserve() and erase()/clear()?


    v.reserve(n) sets the capacity to at least max(n, v.size()) and leaves the
    size unchanged.
    v.erase reduces the size by the number of elements erased and does not
    change the capacity.
    v.clear sets the size to 0 and does not change the capacity.

    > vector<int> v;


    v's size is now 0.

    > v.reserve(100);


    v's size is now 0 and its capacity is >= 100.

    > v.resize(100);


    v's size is now 100 and its capacity is >= 100.

    > v.erase(v.end());


    This is undefined behavior because v.end() does not refer to an element.

    > How many elements are reserved here: 100 or 99?


    > v.reserve(200);


    v's size is now 100 and its capacity is >= 200.

    > v.resize();


    This should not compile, as you did not specify a size.

    > v.clear();


    v's size is now 0 and its capacity is >= 200.
     
    Andrew Koenig, Sep 23, 2005
    #8
  9. Alex Vinokur

    Alex Vinokur Guest

    Alex Vinokur, Sep 23, 2005
    #9
    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. Dilip
    Replies:
    7
    Views:
    667
    Howard Hinnant
    Apr 22, 2006
  2. pkirk25

    vector reserve() and clear()

    pkirk25, Nov 2, 2006, in forum: C++
    Replies:
    3
    Views:
    515
  3. Chris Roth
    Replies:
    2
    Views:
    2,048
    Mark P
    Mar 22, 2007
  4. Mike -- Email Ignored

    std::vector: reserve required?

    Mike -- Email Ignored, Jul 4, 2008, in forum: C++
    Replies:
    22
    Views:
    1,243
    James Kanze
    Jul 10, 2008
  5. mathieu
    Replies:
    12
    Views:
    1,270
    Jeff Flinn
    May 25, 2011
Loading...

Share This Page