vector problem

hi guys
ok so i have a vector called health and it has 10 elements, each element is equal to 100.

the way the program works is if the user type 'd' it will reduce health by 10, and if the health is less than 50 all the element in the vector get erased.

But the problem is i dont know how to erase the element. i used v.erase but that didnt work correctly.

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <vector>

int main()
{
	int tower_health=100;
	char Value;
	bool done=false;
	std::vector<int> health;

	while(health.size()<10)
		health.push_back(tower_health);

	for(int x=0; x<health.size(); x++)
		std::cout<<health.at(x)<<std::endl;

	while(done==false)
	{
		std::cin>>Value;

		if(Value=='d')
		{
			for(int i=0; i<health.size(); i++)
			{
				health.at(i)-=10;

				if(health.at(i)<50)
				{
					health.erase(health.at(i));  //----here is the error
					done=true;
				}
			}
		}

	}

	for(int x=0; x<health.size(); x++)
		std::cout<<health.at(x)<<std::endl;
	

	system("pause");
	return 0;
}
Last edited on
closed account (zb0S216C)
Fantasy wrote:
i used v.erase but that didnt work correctly. (sic)

Did your call to std::vector::erase( ) look like this: MyVector.erase( MyVector.begin( ) + n );

n is the index of the target element.

Wazzak
Last edited on
yes that works but when i do this for example
1
2
if(health.at(i)<50)
MyVector.erase( MyVector.begin( ) + 1 );

it erases the first 5 elements in the vector. its suppose to erase only the 2nd element.
even if i do this
1
2
3
4
5
if(health.at(i)<50)
{
for(int i=0; i<health.size(); i++)
MyVector.erase( MyVector.begin( ) + i );
}

it also erase the first five element, its should erase all the elements
The reason it's erasing multiple elements is because you have it in a loop. What's actually happening is this:

vector: [0123456789] <- the 10 elements in your vector
i=0. erase index 0: [123456789]
loop, so i=1, erase index 1: [13456789]
loop, so i=2, erase index 2: [1356789]
i=3, erase index 3: [135789]
i=4, erase index 4: [13579]
i=5, which is >= vector size (which is now 5), so loop ends

leaving you with 5 elements in the array.

Effectively, you're removing all of the even indexes with that loop.
ooo lol ok thanks that was helpful :)
closed account (DSLq5Di1)
1
2
3
4
std::vector<int> health(10, tower_health); // construct 10 elements with the value of tower_health

	while(health.size()<10)
		health.push_back(tower_health);

vector::erase() invalidates previous iterators and returns a new iterator pointing to the next element,

1
2
3
4
5
6
7
8
9
10
11
12
13
for (auto it = health.begin(); it != health.end();)
{
    *it -= 10;

    if (*it < 50)
    {
        it = health.erase(it);
    }
    else
    {
        it++;
    }
}

std::remove_if() moves removed elements to the back and returns an iterator to the new end,

1
2
3
4
5
6
7
auto end = std::remove_if(health.begin(), health.end(), [](int& hp) -> bool
{
    hp -= 10;
    return hp < 50;
});

health.erase(end, health.end());

http://www.cplusplus.com/reference/algorithm/remove_if/
Topic archived. No new replies allowed.