Searching a vector and deleting the item

I need help writing a piece of code that will search a vector for the object who's mLastName member is the same as the name that the user is searching for. Once found, it must be deleted and the vector must delete the empty space.

Here is what i have so far but the code is not valid at all.
1
2
3
4
5
6
7
8
9
10
11
12
std::string name;
cout << "Enter the last name of the Employee you would like "
     << "to delete: ";
cin >> name;
for (int i = 0, i < database.size; i++)
{
      if(database[i].mLastName == name)
	{
		delete database[i];
		database.pop_back();
	}
}// end for 
in the brackets you want database.erase(database.begin()+i);

the delete keyword doesn't do what you think it does.
Last edited on
i used the delete keyword trying to delete the object that stored in the vector.

And which brackets do i put your code in?
And which brackets do i put your code in?

The ones after if... (the curly ones, mind you)
And after the deed is done, you should break out of the loop (or if you want to remove all matches, you need to decrement i before the next iteration so you don't skip the next element - although in that case std::remove_if is preferable anyway).
Last edited on
now database is underlined red and it says

1
2
3
std::vector<Employe*, std::allocator<Emplyoee*>>database[1]

Error:expression must have class type.


database was declared as
std::vector<Employee*> database[1];
sorry I should've been more clear there
database was declared as

Why? What's up with the array? In any case, you'll have to refer to the sole Database object in the array as database[0].
Athar im not really sure what you mean. BUT, here is my code at the moment.

PROBLEMS
>in the second forloop parameter it says "variable i is not a template" and database must have a constant value
>in the if parenthesis and inside the if it says database must have a class type


1
2
3
4
5
6
7
8
9
10
11
12
13
                        std::string name;
			cout << "Enter the last name of the Employee you would like "
				 << "to delete: ";
			cin >> name;
			for (int i = 0, i < database.size; i++)
			{
				if(database.at(i).mLastName == name)
				{
					database.erase(database.begin()+i;
					database.pop_back();
					break;
				}
			}// end for 
You declared database as an array. Therefore you must specify which element you mean. Since there's just one, that's database[0].

In the code snippet above, you wrote , instead of ; in line 5 and you forgot a closing brace in line 9.
Line 10 needs to go. It removes the last element in the vector, which makes no sense.
Okay i changed the vector from an array. The database i bolded says expression must have a class type. I know that the code is wrong but im not sure how to do it. What im trying to do is

if(object stored in database[i] == name)

my code is
1
2
3
4
5
6
7
8
9
10
11
12
std::string name;
cout << "Enter the last name of the Employee you would like "
	 << "to delete: ";
cin >> name;
for (int i = 0; i < database.size(); ++i)
{
	if(database[i].mLastName == name)
	{
		database.erase(database.begin()+i);
		break;
	}
}// end for 
Since the vector contains pointers, you need to dereference them to get to the actual object, i.e.
database[i]->mLastName...
Thanks!

but now it says mLastName is inaccessible lol.

EDIT:I made the variable public in Employee.h and it works now

But is it bad i did this? if it is is bad there another way around it?
Last edited on
It probably says that because it's inaccessible.
Use a getter.
Oh dude your awesome.

this is the last problem.

i have

if(database[i]->GetLastName() == name)

and it says that no operator '==' matches these operands. wtf is this. Both sides are string are they not?!
Are they? How does the GetLastName() declaration look like?
OH hehe they werent! i had it declared return void instead of string. You rock Athar.


Plus 10 internetz for you
nano511 wrote:
Plus 10 internetz for you

Wow! +10 internets, I only know of one... You must be RICH!!
Idea...

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
45
46
47
48
49
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <list>
using namespace std;



int main()
{
	const int n = 2;
	string myArray[n] = {"forum", "help"}; 

	for(int i = 0; i < n; i++)
	{
	
		cout<<"  "<<myArray[i];

	}

	cout<<"\n\n";

	string remover;
	cout<<"Enter word that you want to be removed form the array: ";
	getline(cin, remover);

	list<string> v(myArray, myArray+n);
	list<string>::iterator it;


	
	v.remove(remover);

	for(it = v.begin(); it != v.end(); it++)
	{
	
		cout<<"  "<<*it;
	
	}

	
	
	
	

	cin.get(); cin.get();
	return 0;
}
Topic archived. No new replies allowed.