last element of list

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

    last element of list

    I'm trying to make a function which insert an object into a list and returns
    an iterator to the new element in the list. But as far as I know there is no
    function which returns an iterator to the last element of a list. (.end()
    returns an iterator one item past the end of the list) How could I solve
    this?


  • Arne Adams

    #2
    Re: last element of list

    "xerix" schrieb[color=blue]
    > I'm trying to make a function which insert an object into a list and[/color]
    returns[color=blue]
    > an iterator to the new element in the list. But as far as I know there is[/color]
    no[color=blue]
    > function which returns an iterator to the last element of a list. (.end()
    > returns an iterator one item past the end of the list) How could I solve
    > this?
    >
    >[/color]
    try std::list<T>::b ack() and make sure that your list is not empty

    Arne


    Comment

    • Arne Adams

      #3
      Re: last element of list

      "xerix" schrieb[color=blue]
      > I'm trying to make a function which insert an object into a list and[/color]
      returns[color=blue]
      > an iterator to the new element in the list. But as far as I know there is[/color]
      no[color=blue]
      > function which returns an iterator to the last element of a list. (.end()
      > returns an iterator one item past the end of the list) How could I solve
      > this?
      >
      >[/color]
      try std::list<T>::b ack() and make sure that your list is not empty

      Arne


      Comment

      • xerix

        #4
        Re: last element of list


        "Arne Adams" <inbox@arneadam s.com> wrote in message
        news:c5918t$tso $06$1@news.t-online.com...[color=blue]
        > "xerix" schrieb[color=green]
        > > I'm trying to make a function which insert an object into a list and[/color]
        > returns[color=green]
        > > an iterator to the new element in the list. But as far as I know there[/color][/color]
        is[color=blue]
        > no[color=green]
        > > function which returns an iterator to the last element of a list.[/color][/color]
        (.end()[color=blue][color=green]
        > > returns an iterator one item past the end of the list) How could I solve
        > > this?
        > >
        > >[/color]
        > try std::list<T>::b ack() and make sure that your list is not empty
        >
        > Arne
        >
        >[/color]

        back returns a reference to the last element from the list, but I need an
        iterator to the last element of the list.


        Comment

        • xerix

          #5
          Re: last element of list


          "Arne Adams" <inbox@arneadam s.com> wrote in message
          news:c5918t$tso $06$1@news.t-online.com...[color=blue]
          > "xerix" schrieb[color=green]
          > > I'm trying to make a function which insert an object into a list and[/color]
          > returns[color=green]
          > > an iterator to the new element in the list. But as far as I know there[/color][/color]
          is[color=blue]
          > no[color=green]
          > > function which returns an iterator to the last element of a list.[/color][/color]
          (.end()[color=blue][color=green]
          > > returns an iterator one item past the end of the list) How could I solve
          > > this?
          > >
          > >[/color]
          > try std::list<T>::b ack() and make sure that your list is not empty
          >
          > Arne
          >
          >[/color]

          back returns a reference to the last element from the list, but I need an
          iterator to the last element of the list.


          Comment

          • Sander

            #6
            Re: last element of list

            "xerix" <xerix@bla.nl > wrote in message
            news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=blue]
            > I'm trying to make a function which insert an object into a list and[/color]
            returns[color=blue]
            > an iterator to the new element in the list. But as far as I know there is[/color]
            no[color=blue]
            > function which returns an iterator to the last element of a list. (.end()
            > returns an iterator one item past the end of the list) How could I solve
            > this?
            >[/color]
            I don't know much about C++, but doesn't give end() an iterator to the
            position where the next object will come? If so, you could first call end()
            and then add the new object.

            Sander


            Comment

            • Sander

              #7
              Re: last element of list

              "xerix" <xerix@bla.nl > wrote in message
              news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=blue]
              > I'm trying to make a function which insert an object into a list and[/color]
              returns[color=blue]
              > an iterator to the new element in the list. But as far as I know there is[/color]
              no[color=blue]
              > function which returns an iterator to the last element of a list. (.end()
              > returns an iterator one item past the end of the list) How could I solve
              > this?
              >[/color]
              I don't know much about C++, but doesn't give end() an iterator to the
              position where the next object will come? If so, you could first call end()
              and then add the new object.

              Sander


              Comment

              • xerix

                #8
                Re: last element of list


                "Sander" <sander_ke@nosp am.hotmail.com> wrote in message
                news:40780c0a$0 $41761$5fc3050@ dreader2.news.t iscali.nl...[color=blue]
                > "xerix" <xerix@bla.nl > wrote in message
                > news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=green]
                > > I'm trying to make a function which insert an object into a list and[/color]
                > returns[color=green]
                > > an iterator to the new element in the list. But as far as I know there[/color][/color]
                is[color=blue]
                > no[color=green]
                > > function which returns an iterator to the last element of a list.[/color][/color]
                (.end()[color=blue][color=green]
                > > returns an iterator one item past the end of the list) How could I solve
                > > this?
                > >[/color]
                > I don't know much about C++, but doesn't give end() an iterator to the
                > position where the next object will come? If so, you could first call[/color]
                end()[color=blue]
                > and then add the new object.
                >
                > Sander
                >
                >[/color]

                That ain't working either :(


                Comment

                • xerix

                  #9
                  Re: last element of list


                  "Sander" <sander_ke@nosp am.hotmail.com> wrote in message
                  news:40780c0a$0 $41761$5fc3050@ dreader2.news.t iscali.nl...[color=blue]
                  > "xerix" <xerix@bla.nl > wrote in message
                  > news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=green]
                  > > I'm trying to make a function which insert an object into a list and[/color]
                  > returns[color=green]
                  > > an iterator to the new element in the list. But as far as I know there[/color][/color]
                  is[color=blue]
                  > no[color=green]
                  > > function which returns an iterator to the last element of a list.[/color][/color]
                  (.end()[color=blue][color=green]
                  > > returns an iterator one item past the end of the list) How could I solve
                  > > this?
                  > >[/color]
                  > I don't know much about C++, but doesn't give end() an iterator to the
                  > position where the next object will come? If so, you could first call[/color]
                  end()[color=blue]
                  > and then add the new object.
                  >
                  > Sander
                  >
                  >[/color]

                  That ain't working either :(


                  Comment

                  • Nick Hounsome

                    #10
                    Re: last element of list


                    "xerix" <xerix@bla.nl > wrote in message
                    news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=blue]
                    > I'm trying to make a function which insert an object into a list and[/color]
                    returns[color=blue]
                    > an iterator to the new element in the list. But as far as I know there is[/color]
                    no[color=blue]
                    > function which returns an iterator to the last element of a list. (.end()
                    > returns an iterator one item past the end of the list) How could I solve
                    > this?
                    >
                    >[/color]

                    list has bidirectional iterators so you can do end()-1


                    Comment

                    • Nick Hounsome

                      #11
                      Re: last element of list


                      "xerix" <xerix@bla.nl > wrote in message
                      news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=blue]
                      > I'm trying to make a function which insert an object into a list and[/color]
                      returns[color=blue]
                      > an iterator to the new element in the list. But as far as I know there is[/color]
                      no[color=blue]
                      > function which returns an iterator to the last element of a list. (.end()
                      > returns an iterator one item past the end of the list) How could I solve
                      > this?
                      >
                      >[/color]

                      list has bidirectional iterators so you can do end()-1


                      Comment

                      • John Harrison

                        #12
                        Re: last element of list


                        "xerix" <xerix@bla.nl > wrote in message
                        news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=blue]
                        > I'm trying to make a function which insert an object into a list and[/color]
                        returns[color=blue]
                        > an iterator to the new element in the list.[/color]

                        The list<T>::insert function already returns an iterator to the newly
                        inserted element.
                        [color=blue]
                        > But as far as I know there is no
                        > function which returns an iterator to the last element of a list. (.end()
                        > returns an iterator one item past the end of the list) How could I solve
                        > this?
                        >[/color]

                        Well I don't really see the connection between your two sentences, but if
                        you really want a function that returns an iterator to the last element of a
                        list then all you have to do is write one. Just use end() to get the one
                        past the end and then operator-- to move back to the end of the last.

                        template <class T>
                        list<T>::iterat or last_element(li st<T>& lst)
                        {
                        list<T>::iterat or i = lst.end();
                        return --i;
                        }

                        Of course bad things will happen if you use this function on an empty list.

                        john


                        Comment

                        • John Harrison

                          #13
                          Re: last element of list


                          "xerix" <xerix@bla.nl > wrote in message
                          news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=blue]
                          > I'm trying to make a function which insert an object into a list and[/color]
                          returns[color=blue]
                          > an iterator to the new element in the list.[/color]

                          The list<T>::insert function already returns an iterator to the newly
                          inserted element.
                          [color=blue]
                          > But as far as I know there is no
                          > function which returns an iterator to the last element of a list. (.end()
                          > returns an iterator one item past the end of the list) How could I solve
                          > this?
                          >[/color]

                          Well I don't really see the connection between your two sentences, but if
                          you really want a function that returns an iterator to the last element of a
                          list then all you have to do is write one. Just use end() to get the one
                          past the end and then operator-- to move back to the end of the last.

                          template <class T>
                          list<T>::iterat or last_element(li st<T>& lst)
                          {
                          list<T>::iterat or i = lst.end();
                          return --i;
                          }

                          Of course bad things will happen if you use this function on an empty list.

                          john


                          Comment

                          • xerix

                            #14
                            Re: last element of list


                            "Nick Hounsome" <nh002@blueyond er.co.uk> wrote in message
                            news:EjUdc.5707 2$Id.417@news-binary.blueyond er.co.uk...[color=blue]
                            >
                            > "xerix" <xerix@bla.nl > wrote in message
                            > news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=green]
                            > > I'm trying to make a function which insert an object into a list and[/color]
                            > returns[color=green]
                            > > an iterator to the new element in the list. But as far as I know there[/color][/color]
                            is[color=blue]
                            > no[color=green]
                            > > function which returns an iterator to the last element of a list.[/color][/color]
                            (.end()[color=blue][color=green]
                            > > returns an iterator one item past the end of the list) How could I solve
                            > > this?
                            > >
                            > >[/color]
                            >
                            > list has bidirectional iterators so you can do end()-1
                            >
                            >[/color]

                            error C2784: 'ptrdiff_t std::operator -(const std::reverse_it erator<_RanIt>
                            &,const std::reverse_it erator<_RanIt> &)' : could not deduce template
                            argument for 'const std::reverse_it erator<_RanIt> &' from
                            'std::list<_Ty, _Ax>::iterator'
                            with
                            [
                            _Ty=int,
                            _Ax=std::alloca tor<int>
                            ]

                            iter = listNum.end()--;

                            This isn't working either.
                            I'm using reverse iterators as a workaround, but that isn't actually what I
                            want.


                            Comment

                            • xerix

                              #15
                              Re: last element of list


                              "Nick Hounsome" <nh002@blueyond er.co.uk> wrote in message
                              news:EjUdc.5707 2$Id.417@news-binary.blueyond er.co.uk...[color=blue]
                              >
                              > "xerix" <xerix@bla.nl > wrote in message
                              > news:7fdae$4078 038b$d47f9535$1 9433@news.multi kabel.nl...[color=green]
                              > > I'm trying to make a function which insert an object into a list and[/color]
                              > returns[color=green]
                              > > an iterator to the new element in the list. But as far as I know there[/color][/color]
                              is[color=blue]
                              > no[color=green]
                              > > function which returns an iterator to the last element of a list.[/color][/color]
                              (.end()[color=blue][color=green]
                              > > returns an iterator one item past the end of the list) How could I solve
                              > > this?
                              > >
                              > >[/color]
                              >
                              > list has bidirectional iterators so you can do end()-1
                              >
                              >[/color]

                              error C2784: 'ptrdiff_t std::operator -(const std::reverse_it erator<_RanIt>
                              &,const std::reverse_it erator<_RanIt> &)' : could not deduce template
                              argument for 'const std::reverse_it erator<_RanIt> &' from
                              'std::list<_Ty, _Ax>::iterator'
                              with
                              [
                              _Ty=int,
                              _Ax=std::alloca tor<int>
                              ]

                              iter = listNum.end()--;

                              This isn't working either.
                              I'm using reverse iterators as a workaround, but that isn't actually what I
                              want.


                              Comment

                              Working...