Jump to content
  • Advertisement
Sign in to follow this  

more issues with removing iteams from a list

This topic is 4131 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

I have a list<int> test that contains data like this 50,50,50,50,50,50,50,50 I need to remove just one of the 50 but if I use an iterator i and then test.erase(i); it erase all the 50's. So how do I just erase one of them at a specific spot in the list?

Share this post


Link to post
Share on other sites
Advertisement
Could you post the code that you're having trouble with? I'm guessing that you have a loop that goes through the vector and erases each element; if you just want to erase one 50 from the vector, you could do something like this:

vector<int>::iterator i;
i = v.begin();
v.erase(i);

Share this post


Link to post
Share on other sites

list<int>::iterator curr = DataList.begin();

list<LoadData::RectX>::iterator curr2 = DataList2.begin();

while(curr != DataList.end()&& curr2 !=DataList2.end())
{
// the position
PoX = *curr;
++curr;
PoY = *curr;
++curr;
left = curr2->left;
top = curr2->top;
right = curr2->right;
bottom = curr2->bottom;
++curr2;


if (CPIR(top_x,top_y,MakePoint(PoX,PoY),MakeRect(left,top,right,bottom)))
{


}



What I want to do is remove the PoX and PoY from DataList but every thing I try does not work

for example I tyred

if (PoX == *i && PoY == *i)DataList.erase(*i)

but of course I will never be the same But I need to remove both of those I teams from the list so next time at that exacte same spot it wont be there and the ball will not collide with what is not there

Share this post


Link to post
Share on other sites
Quote:
Original post by kingpinzs
any one know how I do this?
Based on this thread and your previous threads on the topic, I have a couple of suggests.

1. First, do a clean-up pass on the code. Give your variables and functions names that are more or less self-documenting ('CPIR' is fairly cryptic, and the name 'DataList' conveys very little useful information), and perhaps add some additional comments explaining what the various bits of code are intended to do.

2. Repost your code in its entirety (assuming there's not too much of it, and perhaps with boilerplate stuff such as window creation removed).

This will allow us to get a better overall view of the code and make some reasonable suggestions about changes you might make to the design. There are some problems that are clear from the small excerpt you posted, but I for one would find it easier to suggest alternatives given a more complete picture of what the code is doing (or at the very least, comments or variable and function names that make the intended behavior of the excerpted code more clear).

Share this post


Link to post
Share on other sites
The following cod just test if the conditions are true then returns ture or false
The veribles are ball x and Y and blocks postiotion is P and blocks rect is a

//function is for colliton Point and Rect
bool CPIR(int x,int y,POINT p,RECT a)
{
if((x >= p.x) && (x <= p.x + a.right - 50 ) &&(y>=p.y)&&(y<=p.y + a.bottom))
{
return true;
}
return false;
}





DataList contains all the X and Y values that was pulled from the data file
DataList2 contains the Left, Top , right, bottom coords for the rect info for the blocks

So what I need to do is delete the coorispoding data that is equel to the block the ball just hit so it wont collide with it again


//this is to test if the ball hits the block then it goes to function to delete
//the blcok but it also is supose to delete the iteams from the list so
//it wont collide with a blcok that is not there

int Block_Colition()
{
int top_x = ball.x + ball.width/2;
int top_y = ball.y;

int left_x = ball.x;
int left_y = ball.y + ball.height/2;

int right_x = ball.x + ball.width;
int right_y = ball.y + ball.height/2;

int bottom_x = ball.x + ball.width/2;
int bottom_y = ball.y + ball.height;
int PoX , PoY,left,top,right,bottom = 0 ;

list<int>::iterator curr = DataList.begin();

list<LoadData::RectX>::iterator curr2 = DataList2.begin();

while(curr != DataList.end()&& curr2 !=DataList2.end())
{
// the position
PoX = *curr;
++curr;
PoY = *curr;
++curr;
left = curr2->left;
top = curr2->top;
right = curr2->right;
bottom = curr2->bottom;
++curr2;


if (CPIR(top_x,top_y,MakePoint(PoX,PoY),MakeRect(left,top,right,bottom)))
{
// top = true;
ball.y -= ball.movey;
ball.movey *= -1;
//this removes the block from the screen but not the coordinates used for the collition detection
Remove_list(MakePoint(PoX,PoY),MakeRect(left,top,right,bottom));


}

}



Share this post


Link to post
Share on other sites
test.erase(i) shouldn't be erasing all of the 50s from the list. It should only be removing one 50 from the list (assuming it is pointing to one of the 50s in the list).

I recommend going through as many tutorials on the STL (Standard Template Library) as you can. Since you are working on lists, I recommend going through as many tutorials on lists as you can.

I wrote a little program for you, hopefully it explains things well enough

#include <iostream>
#include <list>

using namespace std;

int main()
{
// creates a list called l
list<int> l;

for (int a = 0; a < 10; a++)
{
//this adds the integers 0 to 9 to l
l.push_back(a);
}

// create an iterator called iter
list<int>::iterator iter;

cout << "Contents of the list at beginning" << endl;
cout << "List size is: " << l.size() << endl;
// this loop iterates through the list and displays the list
for (iter = l.begin(); iter != l.end(); iter++)
{
cout << *iter << endl;
}
// pauses so you can take a look
system("pause");

// set iter so that it equals the beginning of l
iter = l.begin();

// increase the iterator i, so that it now points at the second item in the list l
iter++;

// this removes the item that iter points to, and in this case it removes the
// second item from the list
l.erase(iter);

cout << "\nContents of the list after removing second item" << endl;
cout << "List size is: " << l.size() << endl;
// this loop iterates through the list and displays the list
for (iter = l.begin(); iter != l.end(); iter++)
{
cout << *iter << endl;
}
// pauses so you can take a look
system("pause");

// this erases everything in l
l.clear();

// this checks if l is empty
if (l.empty())
cout << "\nThe list is now empty!" << endl;

// pauses so you can take a look
system("pause");

for (int a = 0; a < 20; a++)
{
//this adds the integer 50 to l 20 times
l.push_back(50);
}

cout << "\nThe new contents of the list after adding twenty 50s to it" << endl;
cout << "List size is: " << l.size() << endl;
// this loop iterates through the list and displays the list
for (iter = l.begin(); iter != l.end(); iter++)
{
cout << *iter << endl;
}
// pauses so you can take a look
system("pause");

// this sets iter equal to the beginning of l
iter = l.begin();

iter++; // increase iter so that it now points at the second item in l
iter++; // increase iter again so that it now points at the third item in l

l.erase(iter);

cout << "\nThe contents of the list after removing the third 50" << endl;
cout << "List size is: " << l.size() << endl;
// this loop iterates through the list and displays the list
for (iter = l.begin(); iter != l.end(); iter++)
{
cout << *iter << endl;
}
// pauses so you can take a look
system("pause");

// this removes all the 50s from the list
l.remove(50);

// this checks if l is empty
if (l.empty())
cout << "\nThe list is now empty!" << endl;

// pauses so you can take a look
system("pause");
}


Also, here is a website that has a good reference on lists. Note it's not a tutorial, just a reference. You should probably try to find a good tutorial, I don't have any that come to mind (other than the ones I gave you in your last thread).
C++ Reference

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!