 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Andreas Guest
|
Posted: Tue Dec 13, 2005 6:09 pm Post subject: Removing items from a list without deleting the item itself? |
|
|
Hi there,
assume I have a number of structure elements filled with some dummy
values...
struct ListElement_st
{ int foo1;
int foo2;
};
ListElement_st ListElement[4];
for(int i=0;i<4;i++)
{ ListElement[i].foo1=i;
ListElement[i].foo2=-i;
}
....and I add these ListElements to a list class object (I am talking
std::list):
list
for (int l=0;l<4;l++)
LinkedList.push_back(ListElement[l]);
As far as I understand, the list class object creates a copy of the
structures passed through push_back(<...>) for the _Myval value in the
_Pnode structure.
Is it possible to remove an element from the list without deleting the
element (structure) itself? I would like to get rid of the element in
the list, but keep an pointer to the element (_Myval) for further
processing without creating another copy.
As far as I had a look in the <list> code, the erase function destroys
both, the link (prev/next) information as well as the payload (_Myval).
I would like to avoid working with a list of pointers as this adds
another level of indirection.
Any idea on how to do this?
Merry Christmas and a Happy New Year!
Cheers,
Andreas
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Carl Barron Guest
|
Posted: Wed Dec 14, 2005 6:13 am Post subject: Re: Removing items from a list without deleting the item its |
|
|
In article <1134465213.715434.181990 (AT) o13g2000cwo (DOT) googlegroups.com>,
Andreas <dg6gai (AT) gmail (DOT) com> wrote:
| Quote: | Hi there,
assume I have a number of structure elements filled with some dummy
values...
struct ListElement_st
{ int foo1;
int foo2;
};
ListElement_st ListElement[4];
for(int i=0;i<4;i++)
{ ListElement[i].foo1=i;
ListElement[i].foo2=-i;
}
...and I add these ListElements to a list class object (I am talking
std::list):
list
for (int l=0;l<4;l++)
LinkedList.push_back(ListElement[l]);
As far as I understand, the list class object creates a copy of the
structures passed through push_back(<...>) for the _Myval value in the
_Pnode structure.
Is it possible to remove an element from the list without deleting the
element (structure) itself? I would like to get rid of the element in
the list, but keep an pointer to the element (_Myval) for further
processing without creating another copy.
As far as I had a look in the <list> code, the erase function destroys
both, the link (prev/next) information as well as the payload (_Myval).
I would like to avoid working with a list of pointers as this adds
another level of indirection.
Any idea on how to do this?
use list<T,A>::splice |
list<ListElement_st> HoldRemovedItems;
// move one item from LinkedList to HoldRemoved Items
// if it points to the item in LinkedList, It will point to the
// same item in HoldRemovedItems.
HoldRemoveItems.splice(HoldRemovedItems.end(),LinkedList,it);
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|