off_the_end iterator to vector::erase() function

Discussion in 'C++' started by subramanian100in@yahoo.com, India, Apr 30, 2008.

  1. , India

    , India Guest

    Consider
    vector<string> v;

    If we call,

    v.erase(v.end())

    this invokes undefined behaviour.

    But, if we call

    v.erase(v.begin(), v.end())
    or
    v.erase(v.end(), v.end())

    will these last two calls invoke undefined behaviour ? Why I am asking
    is that v.clear() on an empty vector is valid - it does nothing.
    v.clear() is equivalent to v.erase(v.begin(), v.end()) which in turn
    is equal to v.erase(v.end(), v.end()) when 'v' is empty. Am I
    correct ?

    Kindly clarify.

    Thanks
    V.Subramanian
    , India, Apr 30, 2008
    #1
    1. Advertising

  2. , India

    Guest

    My views. Guys, correct me if I am wrong.

    ~Moh

    On Apr 30, 9:23 pm, ", India"
    <> wrote:
    > Consider
    > vector<string> v;
    >
    > If we call,
    >
    > v.erase(v.end())
    >
    > this invokes undefined behaviour.
    >

    Correct. Reason: According to 23.1.1 point 3 and 4 operation erase is
    defined on a vector(sequence in general) v as: v.erase(iter) where
    iter is a valid de-referenceable iterator. Clearly, v.end() isn't.

    > But, if we call
    >
    > v.erase(v.begin(), v.end())
    > or
    > v.erase(v.end(), v.end())
    >
    > will these last two calls invoke undefined behaviour ?


    First case will not be undefined as v.begin() and v.end() defines a
    valid range. [I am not dereferencing the end element] this will in
    fact return me a v.end()

    Why I am asking
    > is that v.clear() on an empty vector is valid - it does nothing.
    > v.clear() is equivalent to v.erase(v.begin(), v.end()) which in turn
    > is equal to v.erase(v.end(), v.end()) when 'v' is empty. Am I
    > correct ?


    this should be okay. according to 23.1 we see that begin() == end() in
    case the sequence is empty and we are still providing a valid range.

    >
    > Kindly clarify.
    >
    > Thanks
    > V.Subramanian
    , Apr 30, 2008
    #2
    1. Advertising

  3. <> wrote in message
    news:...

    > Consider
    > vector<string> v;
    >
    > If we call,
    >
    > v.erase(v.end())
    >
    > this invokes undefined behaviour.


    Correct.

    > But, if we call
    >
    > v.erase(v.begin(), v.end())
    > or
    > v.erase(v.end(), v.end())
    >
    > will these last two calls invoke undefined behaviour ?


    No.

    The difference is that when you call v.erase with one argument, you are
    asking it to erase one element. In that case, you must specify a single
    element to erase, and v.end() does not specify an element.

    When you call v.erase with two arguments, you are asking it to erase all of
    the elements in a range. In that case, you must specify a range, and
    passing v.end() twice is one legitimate way of specifying a range that does
    not contain any elements.
    Andrew Koenig, Apr 30, 2008
    #3
  4. , India

    James Kanze Guest

    On Apr 30, 7:53 pm, "Andrew Koenig" <> wrote:
    > <> wrote in message


    > news:...


    > > Consider
    > > vector<string> v;


    > > If we call,


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


    > > this invokes undefined behaviour.


    > Correct.


    > > But, if we call


    > > v.erase(v.begin(), v.end())
    > > or
    > > v.erase(v.end(), v.end())


    > > will these last two calls invoke undefined behaviour ?


    > No.


    > The difference is that when you call v.erase with one
    > argument, you are asking it to erase one element.


    Couldn't one say that logically, "v.erase( i )" is the
    equivalent of "v.erase( i, i + 1 )" (or whatever is necessary to
    obtain an iterator one further in the container). In which
    case, it's rather obvious why "v.erase( v.end() )" is illegal.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, May 2, 2008
    #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. Harald Grossauer

    set<>::erase(iterator) question

    Harald Grossauer, Sep 23, 2003, in forum: C++
    Replies:
    9
    Views:
    4,828
    Harald Grossauer
    Sep 24, 2003
  2. Replies:
    8
    Views:
    1,894
    Csaba
    Feb 18, 2006
  3. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    354
    Pete Becker
    Mar 30, 2006
  4. Anil
    Replies:
    5
    Views:
    428
    Jim Langston
    Dec 18, 2007
  5. thomas
    Replies:
    23
    Views:
    845
    James Kanze
    Feb 26, 2008
Loading...

Share This Page