stl list erase

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paras

    stl list erase


    Hi


    What is the correct way to delete an element from STL list while
    iterating through the list


    list<A*> _ls;
    A * a;
    list<A*>::itera tor si1;
    for (si1=_ls.begin( ); si1!=_ls.end(); ++si1) {

    if ( (*si1)->check() ) {
    a =(*si);
    _ls.erase(si1);
    /* Now this function will point pint si1 to next element. (no
    invalidate) Now how to still continue with for loop which will be a
    problem now ??? ..
    */

    delete a;

    }
    }


  • ES Kim

    #2
    Re: stl list erase

    "Paras" <parsharm@netsc ape.net> wrote in message
    news:3F430157.2 0208@netscape.n et...[color=blue]
    >
    > Hi
    >
    >
    > What is the correct way to delete an element from STL list while
    > iterating through the list
    >
    >
    > list<A*> _ls;
    > A * a;
    > list<A*>::itera tor si1;
    > for (si1=_ls.begin( ); si1!=_ls.end(); ++si1) {
    >
    > if ( (*si1)->check() ) {
    > a =(*si);
    > _ls.erase(si1);
    > /* Now this function will point pint si1 to next element. (no
    > invalidate) Now how to still continue with for loop which will be a
    > problem now ??? ..
    > */
    >
    > delete a;
    >
    > }
    > }[/color]


    list<A*> _ls;
    A* a;
    list<A*>::itera tor si1 = _ls.begin();
    while (si1 != _ls.end())
    {
    if ((*si1)->check())
    {
    a = *si1;
    // si1 points to the next element after erase()
    si1 = _ls.erase(si1);
    delete a;

    }
    else
    ++si1;
    }

    --
    ES Kim


    Comment

    • Naren

      #3
      Re: stl list erase

      Hello,

      erase returns a iterator to first element remaining beyond any elements
      removed.so use this

      I like *while* so i have changed it to while sorry..;-)

      Hope this helps.

      //Code

      list<A*> _ls;
      A * a;
      list<A*>::itera tor si1 = _ls.begin();
      while(si1 !=_ls.end()) {

      if ( (*si1)->check() ) {
      a =(*si);
      si1 = _ls.erase(si1);
      delete a;
      }

      else
      {
      si1++;
      }
      }



      "Paras" <parsharm@netsc ape.net> wrote in message
      news:3F430157.2 0208@netscape.n et...[color=blue]
      >
      > Hi
      >
      >
      > What is the correct way to delete an element from STL list while
      > iterating through the list
      >
      >
      > list<A*> _ls;
      > A * a;
      > list<A*>::itera tor si1;
      > for (si1=_ls.begin( ); si1!=_ls.end(); ++si1) {
      >
      > if ( (*si1)->check() ) {
      > a =(*si);
      > _ls.erase(si1);
      > /* Now this function will point pint si1 to next element. (no
      > invalidate) Now how to still continue with for loop which will be a
      > problem now ??? ..
      > */
      >
      > delete a;
      >
      > }
      > }
      >
      >[/color]


      Comment

      • John Harrison

        #4
        Re: stl list erase


        "ES Kim" <eskim@svd.co.k r> wrote in message
        news:bhuu9g$cat $1@news1.kornet .net...[color=blue]
        > "Paras" <parsharm@netsc ape.net> wrote in message
        > news:3F430157.2 0208@netscape.n et...[color=green]
        > >
        > > Hi
        > >
        > >
        > > What is the correct way to delete an element from STL list while
        > > iterating through the list
        > >[/color][/color]

        [snip][color=blue]
        >
        >
        > list<A*> _ls;
        > A* a;
        > list<A*>::itera tor si1 = _ls.begin();
        > while (si1 != _ls.end())
        > {
        > if ((*si1)->check())
        > {
        > a = *si1;
        > // si1 points to the next element after erase()
        > si1 = _ls.erase(si1);
        > delete a;
        >[/color]

        No good reason for the a varaible

        delete *si1;
        si1 = _ls.erase(si1);
        [color=blue]
        > }
        > else
        > ++si1;
        > }
        >
        > --
        > ES Kim
        >[/color]

        john


        Comment

        • Chris Theis

          #5
          Re: stl list erase


          "Naren" <narendranath.t s@in.bosch.com> wrote in message
          news:bhv0s7$f85 $1@ns2.fe.inter net.bosch.com.. .[color=blue]
          > Hello,
          >
          > erase returns a iterator to first element remaining beyond any elements
          > removed.so use this
          >
          > I like *while* so i have changed it to while sorry..;-)
          >
          > Hope this helps.
          >
          > //Code
          >[/color]
          [SNIP][color=blue]
          >
          > else
          > {
          > si1++;[/color]

          In this case I'd recommend to use ++si1; instead because it might be more
          efficient. Anyway there's nothing to lose if you use prefix instead of
          postfix notation in a case like this.
          [color=blue]
          > }
          > }
          >[/color]

          Chris


          Comment

          • Gavin Deane

            #6
            Re: stl list erase

            Paras <parsharm@netsc ape.net> wrote in message news:<3F430157. 20208@netscape. net>...[color=blue]
            > Hi
            >
            >
            > What is the correct way to delete an element from STL list while
            > iterating through the list
            >
            >
            > list<A*> _ls;
            > A * a;
            > list<A*>::itera tor si1;
            > for (si1=_ls.begin( ); si1!=_ls.end(); ++si1) {
            >
            > if ( (*si1)->check() ) {
            > a =(*si);
            > _ls.erase(si1);
            > /* Now this function will point pint si1 to next element. (no
            > invalidate) Now how to still continue with for loop which will be a
            > problem now ??? ..
            > */
            >
            > delete a;
            >
            > }
            > }[/color]

            Other people appear to have answered your question, but just to point
            out:

            Names beginning with an underscore are reserved for the implementation
            in the global namespace so, depending on context, _ls might not be the
            best name for your list.

            GJD

            Comment

            • Rolf Magnus

              #7
              Re: stl list erase

              Gavin Deane wrote:
              [color=blue]
              > Names beginning with an underscore are reserved for the implementation
              > in the global namespace[/color]

              I thought this only applied if that undersore was followed by an
              uppercase letter.

              Comment

              • Ron Natalie

                #8
                Re: stl list erase


                "Rob Williscroft" <rtw@freenet.RE MOVE.co.uk> wrote in message[color=blue]
                >
                > If I remember/understand correctly, those names and those with two
                > underscores are reserved anywhere, i.e. so the implementor can use
                > them as macro's.[/color]

                And also to set off the decorations from the rest of the identifier (the
                double-underscore prohibition isn't just for leading undersdcores but
                anywhere in the identifier).[color=blue]
                >
                > Identifiers with a single leading underscore are reserved only in
                > the global namespace.[/color]

                NO. Indentifiers with leading underscore and a captial letter are not
                permitted anywhere. If you look at a lot of the template library you'll
                see many implementations use these symbols as their template variables,
                etc...to keep things from colliding with possible user macro definitions.

                It is _ followed by non-uppercase letters that is reserved ONLY in the
                global namespace. These are typically used for certain implementation
                defined global functions etc...



                Comment

                • Rob Williscroft

                  #9
                  Re: stl list erase

                  llewelly wrote in news:86lltoz167 .fsf@Zorthluthi k.local.bar:
                  [color=blue][color=green]
                  >> If I remember/understand correctly, those names and those with two
                  >> underscores are reserved anywhere, i.e. so the implementor can use
                  >> them as macro's.[/color]
                  > [snip]
                  >
                  > Two *consecutive* underscores; i.e. this_is_okay this__is_not
                  >
                  >[/color]

                  Yes, It's what I meant but not what I wrote, Thanks.

                  Rob.
                  --

                  Comment

                  • Gavin Deane

                    #10
                    Re: stl list erase

                    Rob Williscroft <rtw@freenet.RE MOVE.co.uk> wrote in message news:<Xns93DDBF 15546DFukcoREMO VEfreenetrtw@19 5.129.110.201>. ..[color=blue]
                    > llewelly wrote in news:86lltoz167 .fsf@Zorthluthi k.local.bar:
                    >[color=green][color=darkred]
                    > >> If I remember/understand correctly, those names and those with two
                    > >> underscores are reserved anywhere, i.e. so the implementor can use
                    > >> them as macro's.[/color]
                    > > [snip]
                    > >
                    > > Two *consecutive* underscores; i.e. this_is_okay this__is_not
                    > >
                    > >[/color]
                    >
                    > Yes, It's what I meant but not what I wrote, Thanks.
                    >
                    > Rob.[/color]

                    I guess the level of uncertainty here lends weight to the argument
                    that it's simpler just to avoid leading underscores altogether (which
                    is my preference anyway). I checked the standard before my original
                    post in this thread so I'm fairly sure it was right, but the fact that
                    I felt I needed to supports that argument.

                    GJD

                    Comment

                    • Kevin Goodsell

                      #11
                      Re: stl list erase

                      Gavin Deane wrote:[color=blue]
                      >
                      > I guess the level of uncertainty here lends weight to the argument
                      > that it's simpler just to avoid leading underscores altogether (which
                      > is my preference anyway).[/color]

                      I certainly agree with that. I've tried, but never been able to
                      determine for sure what the exact rules are.

                      -Kevin
                      --
                      My email address is valid, but changes periodically.
                      To contact me please use the address from a recent posting.

                      Comment

                      • Gavin Deane

                        #12
                        Re: stl list erase

                        Kevin Goodsell <usenet1.spamfr ee.fusion@never box.com> wrote in message news:<3f441c01@ shknews01>...[color=blue]
                        > Gavin Deane wrote:[color=green]
                        > >
                        > > I guess the level of uncertainty here lends weight to the argument
                        > > that it's simpler just to avoid leading underscores altogether (which
                        > > is my preference anyway).[/color]
                        >
                        > I certainly agree with that. I've tried, but never been able to
                        > determine for sure what the exact rules are.[/color]

                        The rules are in 17.4.3.1 in the standard if you have a copy. Well
                        they're there whether you have a copy or not, but it will be harder
                        for you to look them up if you haven't ;-)

                        And as I said, just because I _can_ look them up doesn't mean I want
                        to every time I choose a variable name, so I steer clear of leading
                        underscores altogether.

                        GJD

                        Comment

                        • Alan Chen

                          #13
                          Re: stl list erase

                          "Naren" <narendranath.t s@in.bosch.com> wrote in message news:<bhv0s7$f8 5$1@ns2.fe.inte rnet.bosch.com> ...[color=blue]
                          > Hello,
                          >
                          > erase returns a iterator to first element remaining beyond any elements
                          > removed.so use this
                          >
                          > I like *while* so i have changed it to while sorry..;-)
                          >
                          > Hope this helps.
                          >
                          > //Code
                          >
                          > list<A*> _ls;
                          > A * a;
                          > list<A*>::itera tor si1 = _ls.begin();
                          > while(si1 !=_ls.end()) {
                          >
                          > if ( (*si1)->check() ) {
                          > a =(*si);
                          > si1 = _ls.erase(si1);
                          > delete a;
                          > }
                          > else
                          > {
                          > si1++;
                          > }
                          > }[/color]

                          There's a bug in this code. If the second to last element is deleted,
                          the last element will never be checked for deletion.

                          Comment

                          • Alan Chen

                            #14
                            Re: stl list erase

                            "Naren" <narendranath.t s@in.bosch.com> wrote in message news:<bhv0s7$f8 5$1@ns2.fe.inte rnet.bosch.com> ...[color=blue]
                            > Hello,
                            >
                            > erase returns a iterator to first element remaining beyond any elements
                            > removed.so use this
                            >
                            > I like *while* so i have changed it to while sorry..;-)
                            >
                            > Hope this helps.
                            >
                            > //Code
                            >
                            > list<A*> _ls;
                            > A * a;
                            > list<A*>::itera tor si1 = _ls.begin();
                            > while(si1 !=_ls.end()) {
                            >
                            > if ( (*si1)->check() ) {
                            > a =(*si);
                            > si1 = _ls.erase(si1);
                            > delete a;
                            > }
                            >
                            > else
                            > {
                            > si1++;
                            > }
                            > }[/color]

                            Nevermind, there is a bug in my brain ... never post w/o morning coffee...

                            Comment

                            Working...