473,386 Members | 1,609 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

two questions about std::list...

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
7 2143
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
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
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
"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
5
by: Eric Lilja | last post by:
Hello, consider this complete program (sorry, it's not minimal but I hope it's readable at least): #include <algorithm> #include <iostream> #include <vector> class Row { public:
44
by: Josh Mcfarlane | last post by:
Just out of curiosity: When would using std::list be more efficient / effective than using other containers such as vector, deque, etc? As far as I'm aware, list doesn't appear to be...
7
by: alex221 | last post by:
In need to implement a tree structure in which every node has arbitrary number of children the following code has come into mind: using std::list; template < class Contents class Tree_node{ ...
8
by: Spoon | last post by:
Hello, Could someone explain why the following code is illegal? (I'm trying to use a list of (C-style) arrays.) #include <list> typedef std::list < int foo_t; int main() { int v = { 12, 34...
0
by: Javier | last post by:
Hi all, I have this code: class A { std::list<Bm_observadores; void function() {
3
by: Ray D. | last post by:
Hey all, I'm trying to pass a list into a function to edit it but when I compile using g++ I continue to get the following error: maintainNeighbors.cpp:104: error: invalid initialization of...
12
by: isliguezze | last post by:
template <class T> class List { public: List(); List(const List&); List(int, const T&); void push_back(const T &); void push_front(const T &); void pop_back();
17
by: Isliguezze | last post by:
Does anybody know how to make a wrapper for that iterator? Here's my wrapper class for std::list: template <class Tclass List { private: std::list<T*lst; public: List() { lst = new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.