two questions about std::list...

Discussion in 'C++' started by SpreadTooThin, Jun 12, 2007.

  1. I want to replace an object in a list with a new object....

    std::list<myObj>::iterator it;

    for (it=mylist.begin(); it != mylist.end(); ++it)
    {
    if (it->compair(myval) == 0)
    {
    *it = newval;
    }
    }

    will *it = newval replace the contents of the list with the new
    data... and hopefully delete the old object?

    How do I insert and object just before the last element in the list?
    SpreadTooThin, Jun 12, 2007
    #1
    1. Advertising

  2. SpreadTooThin

    Andre Kostur Guest

    SpreadTooThin <> wrote in
    news::

    > I want to replace an object in a list with a new object....
    >
    > std::list<myObj>::iterator it;
    >
    > for (it=mylist.begin(); it != mylist.end(); ++it)
    > {
    > if (it->compair(myval) == 0)
    > {
    > *it = newval;
    > }
    > }
    >
    > will *it = newval replace the contents of the list with the new
    > data... and hopefully delete the old object?


    I don't see any dynamic allocation anywhere in here, so where's the
    "delete" question coming from?

    > How do I insert and object just before the last element in the list?


    Um:

    if (!mylist.empty())
    {
    std::list<myObj>::iterator it = mylist.end();

    --it;
    mylist.insert(object, it);
    }
    else
    {
    // List is empty, there is no "last element" in the list
    }
    Andre Kostur, Jun 12, 2007
    #2
    1. Advertising

  3. SpreadTooThin wrote:
    > I want to replace an object in a list with a new object....
    >
    > std::list<myObj>::iterator it;
    >
    > for (it=mylist.begin(); it != mylist.end(); ++it)
    > {
    > if (it->compair(myval) == 0)
    > {
    > *it = newval;
    > }
    > }
    >
    > will *it = newval replace the contents of the list with the new
    > data... and hopefully delete the old object?


    It will not "delete the old object". It will simply invoke

    myObj::eek:perator=(newval);

    (assign new value) for it.

    > How do I insert and object just before the last element in the list?


    There is an 'insert' member function (overloaded) in std::list. RTFM.
    You will need to supply the iterator to the [actual] last item in the
    list (don't confuse it with the iterator returned by 'list::end()').

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 12, 2007
    #3
  4. Andre Kostur wrote:
    > [..]
    > Um:
    >
    > if (!mylist.empty())
    > {
    > std::list<myObj>::iterator it = mylist.end();
    >
    > --it;
    > mylist.insert(object, it);


    mylist.insert(it, object);

    > }
    > else
    > {
    > // List is empty, there is no "last element" in the list
    > }


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 12, 2007
    #4
  5. SpreadTooThin

    Andre Kostur Guest

    "Victor Bazarov" <> wrote in news:f4mrsh$6in$1
    @news.datemas.de:

    > Andre Kostur wrote:
    >> [..]
    >> Um:
    >>
    >> if (!mylist.empty())
    >> {
    >> std::list<myObj>::iterator it = mylist.end();
    >>
    >> --it;
    >> mylist.insert(object, it);

    >
    > mylist.insert(it, object);


    Whups, yep. My bad.

    >> }
    >> else
    >> {
    >> // List is empty, there is no "last element" in the list
    >> }
    Andre Kostur, Jun 12, 2007
    #5
  6. On Jun 12, 1:20 pm, "Victor Bazarov" <> wrote:
    > SpreadTooThin wrote:
    > > I want to replace an object in a list with a new object....

    >
    > > std::list<myObj>::iterator it;

    >
    > > for (it=mylist.begin(); it != mylist.end(); ++it)
    > > {
    > > if (it->compair(myval) == 0)
    > > {
    > > *it = newval;
    > > }
    > > }

    >
    > > will *it = newval replace the contents of the list with the new
    > > data... and hopefully delete the old object?

    >
    > It will not "delete the old object". It will simply invoke
    >
    > myObj::eek:perator=(newval);
    >
    > (assign new value) for it.
    >
    > > How do I insert and object just before the last element in the list?

    >
    > There is an 'insert' member function (overloaded) in std::list. RTFM.
    > You will need to supply the iterator to the [actual] last item in the
    > list (don't confuse it with the iterator returned by 'list::end()').
    >


    After RTFM
    I came up with mylist.insert(mylist.end(), 1, object);
    No good?



    > V
    > --
    > Please remove capital 'A's when replying by e-mail
    > I do not respond to top-posted replies, please don't ask
    SpreadTooThin, Jun 12, 2007
    #6
  7. SpreadTooThin

    Andre Kostur Guest

    SpreadTooThin <> wrote in news:1181679157.426254.202260
    @g37g2000prf.googlegroups.com:

    > On Jun 12, 1:20 pm, "Victor Bazarov" <> wrote:
    >> SpreadTooThin wrote:
    >> > I want to replace an object in a list with a new object....

    >>
    >> > std::list<myObj>::iterator it;

    >>
    >> > for (it=mylist.begin(); it != mylist.end(); ++it)
    >> > {
    >> > if (it->compair(myval) == 0)
    >> > {
    >> > *it = newval;
    >> > }
    >> > }

    >>
    >> > will *it = newval replace the contents of the list with the new
    >> > data... and hopefully delete the old object?

    >>
    >> It will not "delete the old object". It will simply invoke
    >>
    >> myObj::eek:perator=(newval);
    >>
    >> (assign new value) for it.
    >>
    >> > How do I insert and object just before the last element in the list?

    >>
    >> There is an 'insert' member function (overloaded) in std::list. RTFM.
    >> You will need to supply the iterator to the [actual] last item in the
    >> list (don't confuse it with the iterator returned by 'list::end()').
    >>

    >
    > After RTFM
    > I came up with mylist.insert(mylist.end(), 1, object);
    > No good?


    No. As Victor warned you, you want an iterator to the last element in the
    list, not the one-past-the-end element in the list. list<>::insert inserts
    the element immediately before the element at the supplied iterator.
    That's why in my example I had to back up one from the end() iterator (and
    also why I had to check to ensure that the list wasn't empty to begin
    with....).
    Andre Kostur, Jun 12, 2007
    #7
  8. SpreadTooThin wrote:
    > On Jun 12, 1:20 pm, "Victor Bazarov" <> wrote:
    >> SpreadTooThin wrote:
    >>> I want to replace an object in a list with a new object....

    >>
    >>> std::list<myObj>::iterator it;

    >>
    >>> for (it=mylist.begin(); it != mylist.end(); ++it)
    >>> {
    >>> if (it->compair(myval) == 0)
    >>> {
    >>> *it = newval;
    >>> }
    >>> }

    >>
    >>> will *it = newval replace the contents of the list with the new
    >>> data... and hopefully delete the old object?

    >>
    >> It will not "delete the old object". It will simply invoke
    >>
    >> myObj::eek:perator=(newval);
    >>
    >> (assign new value) for it.
    >>
    >>> How do I insert and object just before the last element in the list?

    >>
    >> There is an 'insert' member function (overloaded) in std::list.
    >> RTFM. You will need to supply the iterator to the [actual] last item
    >> in the list (don't confuse it with the iterator returned by
    >> 'list::end()').
    >>

    >
    > After RTFM
    > I came up with mylist.insert(mylist.end(), 1, object);
    > No good?


    You could drop the '1'. However, the effects of

    mylist.insert(mylist.end(), object); // no '1', same difference

    are the same as

    mylist.push_back(object);

    and it _appends_ the value to the list, not inserts it _before_ the
    last element. See Andre's solution.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jun 12, 2007
    #8
    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.

Share This Page