Re: Deleting the last element of an std::set

Discussion in 'C++' started by Jerry Coffin, Jun 28, 2009.

  1. Jerry Coffin

    Jerry Coffin Guest

    In article <4a477c7c$0$1107$>, gbe32241
    @libero.it says...

    [ ... ]

    > At first I've tried with:
    >
    > hi.erase( hi.rbegin() );
    >
    > but there's no overloaded erase function which takes a reverse_iterator
    > as argument, [ ... ]


    You can convert from a reverse iterator to the base type with the
    'base()' member function. Note that rbegin() is basically equivalent
    to end() -- it points one past the end of the set, so you'd need:

    hi.erase((++hi.rbegin()).base());

    --
    Later,
    Jerry.
    Jerry Coffin, Jun 28, 2009
    #1
    1. Advertising

  2. Jerry Coffin

    joseph cook Guest

    On Jun 28, 11:30 am, Jerry Coffin <> wrote:
    > In article <4a477c7c$0$1107$>, gbe32241
    > @libero.it says...
    >
    > [ ... ]
    >
    > > At first I've tried with:

    >
    > > hi.erase( hi.rbegin() );

    >
    > > but there's no overloaded erase function which takes a reverse_iterator
    > > as argument, [ ... ]

    >
    > You can convert from a reverse iterator to the base type with the
    > 'base()' member function. Note that rbegin() is basically equivalent
    > to end() -- it points one past the end of the set, so you'd need:
    >
    > hi.erase((++hi.rbegin()).base());


    Oh no, that's not right!
    rbegin() points to the last element, not end(). It wouldn't be very
    useful to iterate from rbegin() to rend() otherwise, would it?

    Joe Cook
    joseph cook, Jun 28, 2009
    #2
    1. Advertising

  3. Jerry Coffin

    James Kanze Guest

    On Jun 28, 9:49 pm, joseph cook <> wrote:
    > On Jun 28, 11:30 am, Jerry Coffin <>
    > wrote:
    > > In article <4a477c7c$0$1107$>,
    > > gbe32241 @libero.it says...


    > > [ ... ]


    > > > At first I've tried with:


    > > > hi.erase( hi.rbegin() );


    > > > but there's no overloaded erase function which takes a
    > > > reverse_iterator as argument, [ ... ]


    > > You can convert from a reverse iterator to the base type
    > > with the 'base()' member function. Note that rbegin() is
    > > basically equivalent to end() -- it points one past the end
    > > of the set, so you'd need:


    > > hi.erase((++hi.rbegin()).base());


    > Oh no, that's not right!
    > rbegin() points to the last element, not end(). It wouldn't
    > be very useful to iterate from rbegin() to rend() otherwise,
    > would it?


    Formally, rbegin() points to the first element---of the inverted
    sequence. But it does this by containing an iterator to one
    past the last element---this solution is necessary in order for
    rend() to be possible.

    --
    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, Jun 28, 2009
    #3
  4. Jerry Coffin

    Jerry Coffin Guest

    In article <f1636e75-3274-40f7-9bb0-
    >, says...

    [ ... ]

    > Oh no, that's not right!
    > rbegin() points to the last element, not end(). It wouldn't be very
    > useful to iterate from rbegin() to rend() otherwise, would it?


    Yes and no -- when you iterate from rbegin to rend, you do get the
    elements of the set (or whatever) in reverse order, surely enough.
    When you convert rbegin() to its base(), however, what you get is
    basically the same as end() would return -- one past the end of the
    set.

    --
    Later,
    Jerry.
    Jerry Coffin, Jun 29, 2009
    #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. Peter Jansson
    Replies:
    5
    Views:
    6,301
    Ivan Vecerina
    Mar 17, 2005
  2. Vinu
    Replies:
    4
    Views:
    356
    Jim Langston
    Jul 7, 2005
  3. Vinu
    Replies:
    0
    Views:
    351
  4. Simon
    Replies:
    2
    Views:
    489
    Simon
    May 31, 2009
  5. Bo Persson
    Replies:
    2
    Views:
    1,273
    Bart van Ingen Schenau
    Jun 29, 2009
Loading...

Share This Page