By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,132 Members | 1,267 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,132 IT Pros & Developers. It's quick & easy.

pointer iterator question

P: n/a
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
itr = u.erase(itr);
}else {++itr;}
}
}

Dec 26 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a

"JoeC" <en*****@yahoo.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
>I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
if ( (*itr)->marked() ) {
itr = u.erase(itr);
}else {++itr;}
}
}

Dec 26 '06 #2

P: n/a
JoeC wrote:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
if ((*itr)->marked()){
itr = u.erase(itr);
I hope you still have a pointer to your object and delete it somewhere else
in your program.
}else {++itr;}
}
}
Dec 26 '06 #3

P: n/a

Jim Langston wrote:
"JoeC" <en*****@yahoo.comwrote in message
news:11*********************@73g2000cwn.googlegrou ps.com...
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?

if ( (*itr)->marked() ) {
itr = u.erase(itr);
}else {++itr;}
}
}
Thanks, I had seen that before but couldn't find the exact syntax, I
seldom use pointer like that.

Dec 27 '06 #4

P: n/a

JoeC wrote:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
itr = u.erase(itr);
}else {++itr;}
}
}
Erasing the element does not recover any allocation that the element
would be storing. So in the case that unit* is a pointer to something
that was newed - you would have a leak.

Any reason why you aren't using a std::vector of smart pointers to
units? Regardless, i fail to see why you would need erase() anyway.

Dec 27 '06 #5

P: n/a

Salt_Peter wrote:
JoeC wrote:
I am trying to make an iterator work with pointers. I wrote this code
and it works but with an upgrade to my program vector is now unit*
instead of unit type.

void kill(std::vector<unit>& u){
std::vector<unit>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){
itr = u.erase(itr);
}else {++itr;}
}
}

How do I make my new code work?

void combat::kill(std::vector<unit*>& u){
std::vector<unit*>::iterator itr = u.begin();
while(itr != u.end()){
if (itr->marked()){ <-- How doI make this work with pointers?
itr = u.erase(itr);
}else {++itr;}
}
}

Erasing the element does not recover any allocation that the element
would be storing. So in the case that unit* is a pointer to something
that was newed - you would have a leak.

Any reason why you aren't using a std::vector of smart pointers to
units? Regardless, i fail to see why you would need erase() anyway.
You are right, I probibly do have a memory leak. I will take this as a
lesson learned. I know the basics of smart pointers and handles but I
just didn't use them here. The context where I learned about them was
with dynamic binding. I was asking this question in the proscess of
writing a game. I do learn from abstract exaples of programs that
really don't do anything but learn much more from larger projects that
I try to implement ideas and solve problems. I current project is a
re-write and expansion of a similliar project and having a smart
pointer class would have been the right way to go, I just didn't have
any problems untile I wanted to "kill" units as a result of combat. It
got copplicated because I have pointers to those pointers in my map
manager and my combat class.

No that you mention it I could have done much better wich making a
class instead of a vector the handle my units. The creation and
destruction would have been much easier and organized.

I am willing to share the game I wrote, although flawed, I am very
proud of it and also I would like some ideas and advice to make it
better for the next time. I often get told not to be so ambitious and
I outstrip my skill. That is the purpose, I want to exploit my
knowlege to the limit and those problems that arise are the focus on
what to work on next. On another topic I wanted to do sothing but I
said I didn't have the experience to do that and the response I got was
the way to get experience is to do it.

I will see if I can create a unit handle that handles the vector of
pointer and controlls the creation usage and destruction of my units
and will work in the current frame work of my game without too much
re-writing. There is a problem when too much modification is needed
that it is often easier to just re-write it all and plug in what you
can than re-writing code that has unintended consequences.

Dec 28 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.