two questions about std::list...

S

SpreadTooThin

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?
 
A

Andre Kostur

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

Victor Bazarov

SpreadTooThin said:
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
 
V

Victor Bazarov

Andre said:
[..]
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
 
S

SpreadTooThin

SpreadTooThin said:
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?
 
A

Andre Kostur

SpreadTooThin said:
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....).
 
V

Victor Bazarov

SpreadTooThin said:
SpreadTooThin said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top