how can i delete the specific vector?

when the bunny reaches the end of his life i want to delete him from the vector
ive had no trouble deleting the last one, just the specific one


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
vector <bunny>bunnies;
    for (int aa = 0;aa<8;aa++)
   {
        bunnies.push_back(bunny(0,0)) ;  //push back a default constructed bunny.
        bunny &workingBunny = bunnies.back() ;//i have lost track of how to use //reference like this
        workingBunny.setname(namelist()) ;    
        workingBunny.setsex(definesex()) ;
        workingBunny.setposy(getrand());
        workingBunny.setposx(getrand());
        workingBunny.setage();
        brdray [workingBunny.getposx()][workingBunny.getposy()] = (workingBunny.getsex());

    }

getboard();
while (1)//this poor practice is temporary
{
    system ("pause");
    for (int cc=0;cc<bunnies.size();cc++)
    {
        bunnies[cc].getolder();
        bunnies[cc].hopabout();
        bunnies[cc].de_edge(brdray);
        brdray [(bunnies[cc].getposx())][(bunnies[cc].getposy())] = bunnies[cc].getsex();
       if (bunnies[cc].getage()==15)
       {
        //i tried 'bunnies.erase(bunnies[cc])', i think i lost rack on how this //code works too
       }
    }
Last edited on
bunnies.erase(bunnies.begin()+cc);
erase takes an iterator as argument. To convert the index to an iterator you can add the index and the iterator to the first element in the vector.
bunnies.erase(bunnies.begin() + cc);
This will make all the bunnies that is stored after the erased element to be moved one step so that their index is reduced by one. If you go on and do cc++ you will miss one bunny so you probably only want to do cc++ if a bunny was not erased.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (int cc=0;cc<bunnies.size();)
{
	bunnies[cc].getolder();
	bunnies[cc].hopabout();
	bunnies[cc].de_edge(brdray);
	brdray [(bunnies[cc].getposx())][(bunnies[cc].getposy())] = bunnies[cc].getsex();
	if (bunnies[cc].getage()==15)
	{
		bunnies.erase(bunnies.begin() + cc);
	}
	else
	{
		cc++;
	}
}
perfect :) that was easy, i have lots to learn
Topic archived. No new replies allowed.