Vector iterating problem

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:48 PM

Sepp2k, what's your problem? You're splitting hairs for no reason again.

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

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

No problem.
Was This Post Helpful? 1
  • +
  • -

#17 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,318
  • Joined: 25-December 09

Re: Vector iterating problem

Posted 01 January 2013 - 12:53 PM

The problem with invalidating of the iterators when removing an element can be a problem. This is why unless you need random access and you need to remove an item from the middle of the container a std::list may be a better choice. Also the std::list has the remove() function that can remove elements by value.

Jim

This post has been edited by jimblumberg: 01 January 2013 - 12:55 PM

Was This Post Helpful? 1
  • +
  • -

#18 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 - 01:00 PM

View Postsepp2k, on 01 January 2013 - 12:44 PM, said:

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


What would be the correct way of doing it?

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

Was This Post Helpful? 0
  • +
  • -

#19 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,180
  • Joined: 21-June 11

Re: Vector iterating problem

Posted 01 January 2013 - 01:01 PM

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

Sepp2k, what's your problem? You're splitting hairs for no reason again.


Splitting hairs? Either you misunderstood what I said or your definition of hair splitting is baffling. Just to make sure we're on the same page:

If you have a vector that contains consecutive live snowflakes and you use your suggested method of calling erase, there will still be live snowflakes in the vector after the code. That is the code did not actually delete all the live snowflakes (which it was supposed to do).
Was This Post Helpful? 1
  • +
  • -

#20 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 - 01:09 PM

It wasn't supposed to delete all of them, that's absurd. It deletes at a specific point in the vector.

Calm down, take a step back. Reread the requirements in the thread.
Was This Post Helpful? 0
  • +
  • -

#21 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,180
  • Joined: 21-June 11

Re: Vector iterating problem

Posted 01 January 2013 - 01:13 PM

View PostJava Student, on 01 January 2013 - 09:00 PM, said:

What would be the correct way of doing it?


If you need to do it in place, you should use iterators to iterate over the vector and every time you use erase, you should set the iterator the erase's return value rather than incrementing the iterator. So something like this:

vector<snowball>::iterator it = snowballs.begin();
while(it < snowballs.end()) {
    if(it->alive) {
        it = snowballs.erase(it);
    } else {
        it++;
    }
}



That said, I still believe that simply creating a second vector and pushing the living snowflakes into it will be both faster (asymptotically at least) and less error prone.
Was This Post Helpful? 2
  • +
  • -

#22 sepp2k  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2089
  • View blog
  • Posts: 3,180
  • Joined: 21-June 11

Re: Vector iterating problem

Posted 01 January 2013 - 01:19 PM

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

It wasn't supposed to delete all of them, that's absurd. It deletes at a specific point in the vector.

Calm down, take a step back. Reread the requirements in the thread.


I'm calm, but I think there has been a serious misunderstanding in here somewhere. When you say "It deletes ..." you mean one single call to erase, right? If so, then of course you're right, but that's not what I was talking about. I meant that the following loop will not erase all living snowflakes (if there are consecutive ones in the vector):

for(int i = 0; i < snowflakes.size(); i++) {
    if(flakes[i].alive) {
        flakes.erase(flakes.begin() + i);
    }
}



Surely you agree that it is buggy behavior for the above loop to leave living elements in the vector, no?
Was This Post Helpful? 1
  • +
  • -

#23 jimblumberg  Icon User is online

  • member icon


Reputation: 3991
  • View blog
  • Posts: 12,318
  • Joined: 25-December 09

Re: Vector iterating problem

Posted 01 January 2013 - 01:20 PM

Quote

It wasn't supposed to delete all of them, that's absurd. It deletes at a specific point in the vector.

But since this is being done in a loop you will not evaluate the next element, because when you delete the element all the remaining elements shift down. Element i is removed so element i+1 now becomes element i. So on the next iteration of your loop you will have missed an element because of the increment of i. You will also need to change the value of i to account for this shift in position.

But the proper use of iterators is probably the best alternative.

Jim

This post has been edited by jimblumberg: 01 January 2013 - 01:22 PM

Was This Post Helpful? 1
  • +
  • -

#24 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 - 01:49 PM

Thanks for everyone's help! :)
Was This Post Helpful? 0
  • +
  • -

#25 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 - 01:58 PM

View Postjimblumberg, on 01 January 2013 - 08:20 PM, said:

Quote

It wasn't supposed to delete all of them, that's absurd. It deletes at a specific point in the vector.

But since this is being done in a loop you will not evaluate the next element, because when you delete the element all the remaining elements shift down. Element i is removed so element i+1 now becomes element i. So on the next iteration of your loop you will have missed an element because of the increment of i. You will also need to change the value of i to account for this shift in position.

But the proper use of iterators is probably the best alternative.

Jim

Very true. If that is the case either use the iterator as you said, or quit out the loop and start from the beginning, depending on the overall architecture of this part of the game... which wasn't specified.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2