Vector iterating problem

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 1035 Views - Last Post: 01 January 2013 - 01:58 PM Rate Topic: -----

#1 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Vector iterating problem

Posted 01 January 2013 - 01:10 AM

I'll try to explain my problem through code with comments:
struct snowball{
bool alive;
};
vector<snowball*> snowballs;
//fill snowballs with some objects through push_back()...
for(int i=0; i<snowballs.size(); i++){
     
     if(snowballs[i]->alive){
          //delete this element (how to code this?)
     }
}



How do i delete that element?

I have looked into std::vector docs, but the only functions that comes close to doing the job are std::vector::erase() and std::vector::pop_back(). The problem is neither function allows me to specify&delete an element based on a member value.

This post has been edited by Java Student: 01 January 2013 - 01:11 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Vector iterating problem

#2 Coding in the name of  Icon User is offline

  • D.I.C Head

Reputation: 67
  • View blog
  • Posts: 231
  • Joined: 07-July 10

Re: Vector iterating problem

Posted 01 January 2013 - 08:05 AM

In this case the vector stores addresses but not the objects themselves. If you create the objects with new then you have to destroy them with delete.
Was This Post Helpful? 0
  • +
  • -

#3 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: Vector iterating problem

Posted 01 January 2013 - 10:56 AM

Ok, but do you have an idea on how to solve my problem?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Vector iterating problem

Posted 01 January 2013 - 11:22 AM

And what exactly is your problem? Why exactly are you using pointers in your vector? Why are you not just using a "normal" instance of your class in this vector?

Jim
Was This Post Helpful? 1
  • +
  • -

#5 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: Vector iterating problem

Posted 01 January 2013 - 11:51 AM

I think whether the vector type is a pointer or not, the problem and solution remains the same.

My problem is how to loop through the size of the vector and remove any element whose member value alive=true. Basically, how to code the comment:
//delete this element (how to code this?) 


Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Vector iterating problem

Posted 01 January 2013 - 12:05 PM

Quote

I think whether the vector type is a pointer or not, the problem and solution remains the same.

No it is not the same. You have to take extra precautions with the pointer, like calling delete on the memory you allocated with new, before you remove the element from the vector.

Quote

My problem is how to loop through the size of the vector and remove any element whose member value alive=true. Basically, how to code the comment:

You have already stated the method, you iterate through your vector and remove the offending elements.

Now the question is: Is a vector the correct container for your data? There are other containers that may be better suited for this type of operation. A vector is usually a poor choice if you want to insert and remove elements from anywhere but the end of the vector.

Jim
Was This Post Helpful? 1
  • +
  • -

#7 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Vector iterating problem

Posted 01 January 2013 - 12:08 PM

I think you are not understanding what is being said to you here, and stubbornly ignoring such advice is doing you harm.

1. Storing a vector of pointers to snowball is different to storing a vector of snowballs. If you can't see this then you have bigger problems.

2. You use snowballs.erase(snowballs.begin() + i); to erase the element at index 'i'. Of course you use the logic you have implemented to check that the snowball is alive.

This post has been edited by ButchDean: 01 January 2013 - 12:19 PM

Was This Post Helpful? 2
  • +
  • -

#8 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: Vector iterating problem

Posted 01 January 2013 - 12:14 PM

View Postjimblumberg, on 01 January 2013 - 12:05 PM, said:

Quote

I think whether the vector type is a pointer or not, the problem and solution remains the same.

No it is not the same. You have to take extra precautions with the pointer, like calling delete on the memory you allocated with new, before you remove the element from the vector.

O ok, i had a feeling that i would have to do something like that. Thanks, i never knew that.

The only other container i have thought about was a pointer to an array of snowballs, which i thought would get messy, and less cleaner than a vector.

This is a part of a game, where a mouseclick generates a new snowball, and the snowballs is removed once it goes out of bounds or hits an enemy. Which container do you believe best suits my situation?

This post has been edited by Java Student: 01 January 2013 - 12:17 PM

Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Vector iterating problem

Posted 01 January 2013 - 12:18 PM

View PostButchDean, on 01 January 2013 - 08:08 PM, said:

2. You use snowballs.erase(i); to erase the element at index 'i'.


Except that erase doesn't take an index as its argument - it takes an iterator. So he'll have to rewrite his code using iterators instead of indices to iterate the vector.

View PostJava Student, on 01 January 2013 - 08:14 PM, said:

The only other container i have thought about was a pointer to an array of snowballs, which i thought would get messy, and less cleaner than a vector.


Yes, you were right about that. I believe what ButchDean jimblumberg was recommending was a linked list (i.e. std::list).

This post has been edited by sepp2k: 01 January 2013 - 12:26 PM

Was This Post Helpful? 1
  • +
  • -

#10 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 0
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Vector iterating problem

Posted 01 January 2013 - 12:22 PM

Just corrected the vector code. Linked list not really needed.
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Vector iterating problem

Posted 01 January 2013 - 12:22 PM

Quote

This is a part of a game, where a mouseclick generates a new snowball, and the snowballs is removed once it goes out of bounds or hits an enemy. Which container do you believe best suit my situation?

Without seeing the actual implementation it's hard to say, but as a guess I would say possibly a list might be a better alternative. But since I don't normally program games I would only be guessing. However even using std::list I wouldn't necessarily recommend using a pointer. Using pointers should be avoided whenever possible, that is one of the reasons you are using the standard containers in the first place to make things easier. But if you do decide you need pointers for some reason, make sure you wrap the container in a class to help insure proper cleanup.

Jim
Was This Post Helpful? 1
  • +
  • -

#12 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Vector iterating problem

Posted 01 January 2013 - 12:33 PM

Personally I'd probably just populate second vector with the snowballs that are still alive and leave the original vector unchanged. That way you don't get the quadratic performance of calling erase repeatedly, but avoid the overhead of using a linked list when iterating over or otherwise working with the list.

View PostButchDean, on 01 January 2013 - 08:22 PM, said:

Just corrected the vector code. Linked list not really needed.


No, that's not correct. If you iterate by index and only create iterators by arithmetic to use with erase, you're going to skip over elements. You need to use iterators to do the iterating and use the return value of erase.
Was This Post Helpful? 0
  • +
  • -

#13 Java Student  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 480
  • Joined: 05-February 10

Re: Vector iterating problem

Posted 01 January 2013 - 12:36 PM

Thanks Butch, "snowballs.erase(snowballs.begin()+i);" works perfect

Jim, if i wanted to not use a pointer instead, would these be the proper changes:
vector<snowball*> snowballs;

//in mouse function
snowballs.push_back(new snowball());
//out of mouse function


into->
vector<snowball> snowballs;

//in mouse function
snowball s;
snowballs.push_back(s);
//out mosue function



Another question Jim, you say i should avoid pointers whenever possible, but shouldn't i use pointers for large data since the heap is vastly larger than the stack?

This post has been edited by Java Student: 01 January 2013 - 12:38 PM

Was This Post Helpful? 0
  • +
  • -

#14 jimblumberg  Icon User is offline

  • member icon


Reputation: 4003
  • View blog
  • Posts: 12,354
  • Joined: 25-December 09

Re: Vector iterating problem

Posted 01 January 2013 - 12:42 PM

Quote

if i wanted to not use a pointer instead, would these be the proper changes:

Looks about right.

Quote

you say i should avoid pointers whenever possible, but shouldn't i use pointers for large data since the heap is vastly larger than the stack?

The standard containers use heap to store the data.

Jim
Was This Post Helpful? 1
  • +
  • -

#15 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2100
  • View blog
  • Posts: 3,197
  • Joined: 21-June 11

Re: Vector iterating problem

Posted 01 January 2013 - 12:44 PM

View PostJava Student, on 01 January 2013 - 08:36 PM, said:

Thanks Butch, "snowballs.erase(snowballs.begin()+i);" works perfect


No, it does NOT. If you have live snowballs next to each other, the odd ones will be skipped.

Quote

Another question Jim, you say i should avoid pointers whenever possible, but shouldn't i use pointers for large data since the heap is vastly larger than the stack?


The elements of a vector will be stored on the heap whether you use pointers or not. The only difference is that with a vector of objects, the objects will live next to each other on the heap, while with a vector of pointers, only the pointers will live next to each other on the heap and the pointed-to objects (assuming they were allocated with new) will be scattered all over the place.
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2