C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Removing items from a list without deleting the item itself?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Andreas
Guest





PostPosted: Tue Dec 13, 2005 6:09 pm    Post subject: Removing items from a list without deleting the item itself? Reply with 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?

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





PostPosted: Wed Dec 14, 2005 6:13 am    Post subject: Re: Removing items from a list without deleting the item its Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.