By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,346 Members | 2,339 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,346 IT Pros & Developers. It's quick & easy.

two questions about std::list...

P: n/a
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?

Jun 12 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
SpreadTooThin <bj********@gmail.comwrote in
news:11**********************@d30g2000prg.googlegr 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
}

Jun 12 '07 #2

P: n/a
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::operator=(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
Jun 12 '07 #3

P: n/a
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
Jun 12 '07 #4

P: n/a
"Victor Bazarov" <v.********@comAcast.netwrote 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
}
Jun 12 '07 #5

P: n/a
On Jun 12, 1:20 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
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::operator=(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

Jun 12 '07 #6

P: n/a
SpreadTooThin <bj********@gmail.comwrote in news:1181679157.426254.202260
@g37g2000prf.googlegroups.com:
On Jun 12, 1:20 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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::operator=(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....).
Jun 12 '07 #7

P: n/a
SpreadTooThin wrote:
On Jun 12, 1:20 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
>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::operator=(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
Jun 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.