Classes and Vectors

Hi iv go this problem with this program Im trying to write. In it I have created a class which has a vector which stores pointers of another class.

eg.
1
2
3
4
5
6
7
8
9
10
11
class Company
{

private:
	
	vector<StaffMember*> employees;

public:
	// etc

};


I'm trying to delete specific things within the vector. And I know that I should create a function that does this within the company class. But how do I write it? Any pointers? (lol, not funny but meh)
Use an iterator vector<StaffMember*>::iterator to find the StaffMember you want to delete, then delete the StaffMember instance the StaffMember * is pointing to (otherwise you will get a memory leak), then finally call the erase method of vector with the iterator to delete the pointer.
Have a look here for more info on vectors http://www.cplusplus.com/reference/stl/vector/. Have a go, if you get stuck, post your code here and someone will try to help

Bertha
Thanks Bertha.

Well I looked through the tutorial and Im still a little fuzzy on the details.
Heres what I have written:

This is one of the member functions in the company class which returns 1 once a employee is deleted in the vector.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	
bool Company::deletePerson( int id )
{	
	
	for(int i = 0; i < employees.size(); i++ )
	{
		StaffMember* p = employees[i];
		if( p->getID() == id )
		{
			employees.erase(employees[i]);
			return 1;
		}
			
	}
	return 0;
}


I know I'm missing certain parts to this code to make it work but I just dont know what to put in. Can someone please give me some extra pointers?

Thanks.
The basic constructs you are using are fine, with the for loop and condition for the erase call. Unfortunately the erase method will only take iterator(s) as parameter(s).
There are a couple of ways you could do this, either way you need an iterator
 
vector<StaffMember*>::iterator iter;

The first way is to leave your loop control as it is. If you go for this approach you also need to increment the iterator as you loop. Initialise the iterator to the start of you vector
 
iter = employees.begin();

and use the ++ operator to move it through the vector as you loop.
Alternatively use the iterator to control your for loop and ditch the int
 
for (iter = employees.begin(); iter != employees.end(); iter++)

Remember the iterator returned by the end() method is not the last item in the vector, it's off the end hence the !=. Less than does not work as each iterator position has no 'greater than' or 'less than' relationship with its neighbours. In contrast, the begin() method does point to the first item in the vector.
If you need it, you can get at the StaffMember * by dereferencing the iterator so for the next bit, you could get p from the iterator
 
StaffMember* p = *iter;

or you could go straight for
 
if (*iter->getID == id)


In both approaches you will need to call erase with iter rather than employees[i]

Go with the approach you're most comfortable with.

One last thing. You need to add in the delete as I mentioned in my previous post before calling erase



Last edited on
Topic archived. No new replies allowed.