how does map take care invalid range for erase()?

Discussion in 'C++' started by newbie, Jul 27, 2007.

  1. newbie

    newbie Guest

    Does standard say anything about this situation? or it is an undefined
    behavior?

    Thanks

    ------------------------------
    map<int, int> test;
    test[1] = 1;
    test[2] = 2;
    test[3] = 3;

    test.erase(test.find(1), test.find(2)); // basically this is an
    invalid range to erase.

    map<int, int>::iterator pos = test.begin();
    test.erase(pos, --pos); // another example of invalid range.
    ----------------------------
    newbie, Jul 27, 2007
    #1
    1. Advertising

  2. newbie

    Andre Kostur Guest

    newbie <> wrote in news:1185514036.005960.85290
    @g12g2000prg.googlegroups.com:

    > Does standard say anything about this situation? or it is an undefined
    > behavior?
    >
    > Thanks
    >
    > ------------------------------
    > map<int, int> test;
    > test[1] = 1;
    > test[2] = 2;
    > test[3] = 3;
    >
    > test.erase(test.find(1), test.find(2)); // basically this is an
    > invalid range to erase.


    That's invalid? Perhaps you meant find(2) then find(1) ?

    > map<int, int>::iterator pos = test.begin();
    > test.erase(pos, --pos); // another example of invalid range.
    > ----------------------------



    Yep, they're both Undefined Behaviour. It's the programmer's
    responsibility to ensure that the ranges passed to erase() (as in: the
    first iterator is "less than" or equal to the second iterator.
    Andre Kostur, Jul 27, 2007
    #2
    1. Advertising

  3. newbie

    newbie Guest

    On Jul 27, 6:38 am, Andre Kostur <> wrote:
    > newbie <> wrote in news:1185514036.005960.85290
    > @g12g2000prg.googlegroups.com:
    >
    > > Does standard say anything about this situation? or it is an undefined
    > > behavior?

    >
    > > Thanks

    >
    > > ------------------------------
    > > map<int, int> test;
    > > test[1] = 1;
    > > test[2] = 2;
    > > test[3] = 3;

    >
    > > test.erase(test.find(1), test.find(2)); // basically this is an
    > > invalid range to erase.

    >
    > That's invalid? Perhaps you meant find(2) then find(1) ?

    Right, I meant the other way. test.erase(test.find(2), test.find(1));
    >
    > > map<int, int>::iterator pos = test.begin();
    > > test.erase(pos, --pos); // another example of invalid range.
    > > ----------------------------

    >
    > Yep, they're both Undefined Behaviour. It's the programmer's
    > responsibility to ensure that the ranges passed to erase() (as in: the
    > first iterator is "less than" or equal to the second iterator.


    Thanks for the help
    newbie, Jul 27, 2007
    #3
    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. Replies:
    4
    Views:
    8,036
    gulin
    Jun 9, 2008
  2. erase vs. erase

    , Mar 25, 2006, in forum: C++
    Replies:
    7
    Views:
    354
    Pete Becker
    Mar 30, 2006
  3. yaru22
    Replies:
    2
    Views:
    262
    Victor Bazarov
    Jun 23, 2007
  4. xiang chen
    Replies:
    2
    Views:
    607
    The Ghost in The Machine
    May 14, 2011
  5. Tomoyuki Kosimizu

    Range does not take an Range object.

    Tomoyuki Kosimizu, Nov 25, 2003, in forum: Ruby
    Replies:
    3
    Views:
    138
    Tomoyuki Kosimizu
    Nov 27, 2003
Loading...

Share This Page