• Advertisement
Sign in to follow this  

How to delete the complete list

This topic is 4044 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If DataList2 is the std::list wont the fallowing code delete all of it? Because I am trying and it does not seam to be doing the trick. DataList2.erase (DataList2.begin(), DataList2.end());

Share this post


Link to post
Share on other sites
Advertisement
It should. However, use the member function clear to clear the entire list instead of using the function taking an arbitrary iterator range.

Share this post


Link to post
Share on other sites
As above, use clear().

Note that if the type contained with the list is a pointer, then delete will not be called on the pointers. The list cleans up what it allocates; you must clean up what you allocate.

Share this post


Link to post
Share on other sites
Quote:
Original post by jpetrie

Note that if the type contained with the list is a pointer, then delete will not be called on the pointers. The list cleans up what it allocates; you must clean up what you allocate.


How would I do that?

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
How would I do that?
Iterate through the container, and delete each element.


std::list <Type *> Data;

for(std::list <Type *> ::iterator iIndex = Data.begin(), iTail = Data.end(); iIndex != iTail; ++ iIndex)
delete * iIndex;

Data.clear();


Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
Is there another way to delete it with out using a pointer?
I'm not sure I understand what you mean. You only have to manually do what's outlined above if your container stores pointers in the first place.

Share this post


Link to post
Share on other sites
I belive that this delete * iIndex; means a pointer to iIndex but I am not sure though. I tryed just delete iIndex it gave me error
type `struct std::_List_iterator<int>' argument given to `delete', expected pointer

Share this post


Link to post
Share on other sites
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.

Share this post


Link to post
Share on other sites
in this function

void Add(POINT pos,RECT Src)
{

paddle.Pos=pos;

paddle.Src=Src;

Sprites.push_back(paddle);

}

how could I remove pos and src instead of adding it?

Share this post


Link to post
Share on other sites
he was saying you delete the elements yourself IF they are pointers (and hence you allocated them yourself).

The normal rule is, if you new it, you delete it ... if you don't new it, you don't delete it ... (unless you call code which new's it for you but that's a different sroty).

Simple example:


vector<MyClass> things;
vector<MyClass*> things2;

//... do stuff adding items and such ...

//for things:
things.clear();

//for things2:
for(vector<MyClass*>::iterator pos = things2.begin(), things2.end(); ++pos)
{
delete *pos;
*pos = 0; // or null of course
}
things2.clear();



so you see, in the pointer version, you have to destroy your items manually ... because the destruction of a pointer does not automatically call delete for you (for good reason ... many many times you don't want to delete ... only the OWNER of a pointer should delete it).

Share this post


Link to post
Share on other sites
Quote:
Original post by raz0r
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.


No, you shouldn't.

The * operator is used to dereference the pointer, and let the compiler know that you are creating a pointer when you declare it. The * is not actually part of the name.

Share this post


Link to post
Share on other sites
Quote:
Original post by programwizard
Quote:
Original post by raz0r
You need to dereference the iterator to properly delete your data. In other words, "delete * iIndex" is what you should be using.


No, you shouldn't.

The * operator is used to dereference the pointer, and let the compiler know that you are creating a pointer when you declare it. The * is not actually part of the name.


Yes, you should

The * operator is being used here to dereference the iterator to get the pointer to delete. That is, to actually dereference the pointer, you'd need to do *(*iIndex). Of course, you'd normally just do *iIndex->, but I felt that the emphasis was necessary.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
The issue is right now there not pointers. So do I have to make them Pointers?


No. If you're using something like std::list<*myType> then you have to manually delete the elements stored in the list before clearing the list or you'll leak memory. If you're using something more like std::list<myType> then the list will delete the elements itself when you clear it.

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
The issue is right now there not pointers. So do I have to make them Pointers?


Does the list contain pointers? If it does, then you have to delete them before you clear() the list. If the list just contains normal objects (and it looks like it does from the code you posted), just call list.clear()

EDIT:

Quote:
Original post by TheUnbeliever
The * operator is being used here to dereference the iterator to get the pointer to delete.


Ah, so the iterator is a pointer to a pointer? Thanks for clearing that up.

Share this post


Link to post
Share on other sites
no, it is not a pointer, but it is like a pointer. If you have an iterator into a list you can dereference it to get to the data stored in one of the nodes. You can also increment the pointer. Then when you dereference it, it will give you the data in the next node. A pointer to a node or to the data in a node wouldn't work that way.

Share this post


Link to post
Share on other sites
How can I change this

void Add(POINT pos,RECT Src)
{

paddle.Pos=pos;

paddle.Src=Src;

Sprites.push_back(paddle);

}

to just remove the paddle.Pos from the list<SPRITE> Sprites;

the struct is


typedef struct {
int x,y;
int width,height;
int movex,movey;
int curframe,lastframe;
int animdelay,animcount;
int scalex, scaley;
int rotation, rotaterate;
POINT Pos;
RECT Src;
} SPRITE;


Share this post


Link to post
Share on other sites
I am guessing this is due to a translation error of 'remove'. Is this what you want to do?
POINT thePointYouWant = Sprites[0].Pos;

If you are trying to remove a single member element from the struct then that isn't possible. You can reset it to a default value but not remove it from memory.

Share this post


Link to post
Share on other sites
AS far as I know I just want to remove a member from the stl::list and the member type is the struct.

Share this post


Link to post
Share on other sites
I run a company that makes phone books. One day, someone calls up and tells us he doesn't want his phone number listed any more. He doesn't leave a name, but we get his phone number from caller ID. How do I remove him from the phone book?

Answer: The numbers in the phone book aren't sorted in any way; the names are, so the phone number in question could be anywhere. We have to *find* it, by looking at each entry in the phone book one at a time until we see the number we want. Then, given the position in the phone book, we can just remove the entry at that position.

In C++, we can find things using the standard library algorithm std::find. This will provide an iterator to the first found-element, if there is one, or to the "end" of the container otherwise. We can then provide that iterator to the 'erase' member function of the container.

If there is potentially more than one thing to remove at a time, though, and you want to remove all the Sprites with the indicated position, there are more sophisticated algorithms to use (such as the so-called "erase-remove idiom").

On the other hand, if you will *commonly* try to access, remove etc. sprites from the container given just their *position*, then a std::list may be the wrong container for you. You may want an *associative* container, such as a std::map. This basically would have you use the position of the object as an index into the container.

Share this post


Link to post
Share on other sites

Did you know that the line Sprites.push_back(paddle) is not putting paddle directly into the list? It is making a copy of paddle, with whatever values it had at that time, inside the list. If you later change a value of paddle, the one in the list will not change. If you change the one in the list, paddle will not change. If you remove the one from the list entirely, paddle will still exist with whatever values it had before.

Maybe you already know this. I can only guess because you haven't really explained what is supposed to happen, and what are the symptoms that led you to think "something is wrong here". You only asked how to remove something from a list. What you are trying to fix may not be the cause of the problem.

Share this post


Link to post
Share on other sites
Zahlman Thank you for that explanation because I had no idea that is what was going on or needed. fI wish the books would explain it that way.

Hollower I did not know that either. I thought that I could erase or remove the iteam from the list and I would be good.

So let me see if I understand this.

Call the class global verible
LoadData GameData;

then inside initializegame function I call

Load data from the text file to the list that is inside the class
GameData.LoadConfig(image.txt);

then

Load the X and Y from the list inside the class to local list
DataList=GameData.Body();

then

Load the RECT info from the list inside the class to local list
DataList2=GameData.RectLR();

then I call the function
add_blocks();

Loads the data from the above two list to a single list using

void Add(POINT pos,RECT Src)
{
paddle.Pos=pos;
paddle.Src=Src;

Sprites.push_back(paddle);
}

NOTE: I use two functions to take my list and make points and rect out of them

then in the main loop
I call the collition function
were all my issues are I belive

I go through both list pulling out the data

then I test it againts the ball position which works fine

this were the issue comes in
I am trying to destroy the block that the ball hit


So let me see if I understand this.

I have to go through the list finding the iteams I want to delete then delete it then I need to clear the data from the struct at the same time

I was trying to delete the iteams from the first two list but I see now that was wrong. I need to go through the Sprite list finding the data and removing it from there and also delete the data from the struct is that right?


then I call the draw function


void Blocks_Map()
{
for(list<SPRITE>::iterator i=Sprites.begin();i!=Sprites.end();++i)
{
D3DXVECTOR3 position(i->Pos.x,i->Pos.y,0);

sprite_handler->Draw(
blocks_image,
&(i->Src),
NULL,
&position,
D3DCOLOR_XRGB(255,255,255));
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement