Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > two questions about std::list...

Reply
Thread Tools

two questions about std::list...

 
 
SpreadTooThin
Guest
Posts: n/a
 
      06-12-2007
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?

 
Reply With Quote
 
 
 
 
Andre Kostur
Guest
Posts: n/a
 
      06-12-2007
SpreadTooThin <(E-Mail Removed)> wrote in
news:(E-Mail Removed) oups.com:

> 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
}



 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      06-12-2007
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: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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-12-2007
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


 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      06-12-2007
"Victor Bazarov" <(E-Mail Removed)> 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
>> }

 
Reply With Quote
 
SpreadTooThin
Guest
Posts: n/a
 
      06-12-2007
On Jun 12, 1:20 pm, "Victor Bazarov" <(E-Mail Removed)> 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: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



 
Reply With Quote
 
Andre Kostur
Guest
Posts: n/a
 
      06-12-2007
SpreadTooThin <(E-Mail Removed)> wrote in news:1181679157.426254.202260
@g37g2000prf.googlegroups.com:

> On Jun 12, 1:20 pm, "Victor Bazarov" <(E-Mail Removed)> 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: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....).
 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      06-12-2007
SpreadTooThin wrote:
> On Jun 12, 1:20 pm, "Victor Bazarov" <(E-Mail Removed)> 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: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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
(8-bit binary to two digit bcd) or (8-bit binary to two digit seven segment) Fangs VHDL 3 10-26-2008 06:41 AM
How to compare two SOAP Envelope or two Document or two XML files GenxLogic Java 3 12-06-2006 08:41 PM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM
Two ISP -Two Routers - 1 PIX James Parks Cisco 5 12-11-2003 08:55 PM
Cisco 2611 with Two T1 from two ISPs Adam Embrey Cisco 3 07-24-2003 10:22 PM



Advertisments