how to identify only one data member from vector?

for example i have a vector of string, int and char. i want to make a program that gets the users input and if the input matches one of the element of the data member in the vector, it will erase that vector.

lets say for example the code; (the code doesnt work btw)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class human{
 string name;
 int id;
 char gender;
}

vector <human> v;
human h;

v.push_back(h("",1,m));
v.push_back(h("'.2,f));

cout << "Enter ID: ";
cin >> id

for (int i = 0; v[i] == id; i++)
 erase.v[i]; 


how do i make it so that id could be use to identify the vector?
Last edited on
You could use a combination of http://cplusplus.com/reference/algorithm/find/ and http://cplusplus.com/reference/stl/vector/erase/
Or, you could write a loop of your own, but that would be a bit more complex and you seem to be having enough problem without it..


Oh wait. I see you want to find by id. You'd have to use find_if then and that would be even more complicated..
Here's a loop:
1
2
3
4
5
for(vector<human>::iterator i = v.begin(); i != v.end(); i++)
   if(i->id == id) {
      v.erase(i);
      break;
   }
Note that this code only erases one element. To make it work well with more elements with the same id, you'd need some more safeguards (where does 'i' point when you erase it?).
Last edited on
To remove all elements from the vector that match your condition, use remove_if/erase

Modern approach is to use a lambda-expression

1
2
3
    v.erase( remove_if(v.begin(), v.end(), [id](const human& h){
                                return h.id == id;
                            }), v.end());

demo: http://ideone.com/qZpDV

old-timer approach is a functor:

1
2
3
4
5
6
7
struct CheckID {
    int id;
    CheckID(int i) : id(i) {}
    bool operator()(const human& h) const { return id == h.id; }
};
...
    v.erase( remove_if(v.begin(), v.end(), CheckID(id)), v.end());

demo: http://ideone.com/u7nr1
Last edited on
i managed to do it using the iterator and erase method from hamsterman. what i did was;

1
2
3
4
5
6
int target;
cout << "which to delete? ";
cin >> target;

for (it = v.begin(); it != v.end(); i++)
    v.erase (v.begin()+target);
Huh? You're just deleting target'th element here. You don't need a for loop for that. Just line 6. I thought you wanted to find the element with the right id?
yeah, initially i wanted to do that, but it didnt work. it always gives this kind of error

z.cpp: In member function 'void room::printMALE(char)':
z.cpp:75:12: error: request for member 'gender' in '*((room*)this)->room::it.__g
nu_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = s
tudent**, _Container = std::vector<student*>, __gnu_cxx::__normal_iterator<_Iter
ator, _Container>::pointer = student**]()', which is of non-class type 'student*
'

in the end i decide to add a number infront of the vectors so the user can input which to erase based on the numbers.

this is working fine for me

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
#include <iostream>
#include <string>
using namespace std; 

class human
{
	private:
			string name ;	
			int id ; 
			char gender ; 
	public:
			human(){}
			human( string n , int i , char g) : name(n) , id (i) , gender(g) { } 
			~human() {}
			void display () ;
			int getid() { return id ; }
};
void human::display()
{
		cout<<"\n "<<name<<"\t"<<id<<"\t"<<gender; 
}
int main()
{
	vector<human> v; 
	v.push_back(human ("Jimmi", 13, 'm') );
	v.push_back(human ("Lomi", 15, 'g') );
	v.push_back(human ("Jomi", 23, 'e') );

	for( vector<human>::iterator i = v.begin() ; i != v.end() ; i++ )
	{
		if( i->getid() == i_d )
		{
			v.erase(i);
			break;
		}
	}
tried again, but it still whos error

z.cpp: In member function 'void room::delstudent(int)':
z.cpp:88:12: error: request for member 'getid' in '*((room*)this)->room::it.__gn
u_cxx::__normal_iterator<_Iterator, _Container>::operator-> [with _Iterator = st
udent**, _Container = std::vector<student*>, __gnu_cxx::__normal_iterator<_Itera
tor, _Container>::pointer = student**]()', which is of non-class type 'student*'

im actually using vectors pointers in class so that might be the difference.
here's the code for the class i tried using it with

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
50
51
52
53
54
55
class room
{
private:
	vector<student*> students;
	vector<student*>::iterator it;
	vector<student*> MA1;
	vector<student*> MA2;
	vector<student*> MA3;
	vector<student*> MA4;
	vector<student*> MA5;
	vector<student*> MA6;
	vector<student*> MA7;
	vector<student*> MA8;
	int capacity;
	char block;
	
public:
	~room() {
		for (it = students.begin(); it != students.end(); it++)
			delete *it;
	}
	void addstudent(student* s)
	{
		students.push_back(s);
	}
	void addMA1(int target)
	{
		MA1.push_back(students[target]);
		students[target]->setstatus("MA1");
	}
	void delMA1(int target)
	{
		MA1.erase(MA1.begin()+target);
		students[target]->setstatus("UNASSIGNED");
	}
	void delstudent (int target)
	{
		for(it = students.begin(); it != students.end() ; it++)
		{
			if (it->getid() == target)
				students.erase(it);
		}
	}
	void printALL()
	{	
		for(int i = 0; i < students.size(); i++)
			students[i]->print(i);
	}
	void printMA1()
	{
		for(int i = 0; i < MA1.size(); i++)
			MA1[i]->print(i);
	}
	int studentsize(){return students.size();}
};
ok, as it turns out, using my method causes alot of complications.. if it's possible, can someone teach me how the method would work with my codes?

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <iostream>
#include <vector>
#include <cstdlib>
#include <set>
#include <algorithm>
using namespace std;

class room;

class student
{
private:
	room* Room;
	string name, dummy, status;
	char gender;
	int ID;
	
public:
	student() : name("UNKNOWN"), ID(0), gender('-'), status("UNASSIGNED"){}
	student (string Name, int ID, char Gender)
	{
		this->name = Name;
		this->ID = ID;
		this->gender = Gender;
	}
	student(const student& s): name(s.name), ID(s.ID), gender(s.gender){}
	void setstudent();
	void setstatus(string status) {this->status = status;}
	void print(int i) const
	{
		cout << i+1 << ". ID: " << ID << " | NAME: " << name << 
		" l SEX: " << gender << " l ROOM: " << status << endl;
	}
	void print2(int i, int target) const
	{
		cout << i+target << ". ID: " << ID << " | NAME: " << name << 
		" l SEX: " << gender << " l ROOM: " << status << endl;
	}
	int getid(){return ID;}
};

void student::setstudent()
{
	cout << "Enter student's ID: ";
	cin >> ID;
	cout << "Enter student's gender (M/F): ";
	cin >> gender;
	gender = toupper(gender);
	getline(cin, dummy);
	cout << "Enter student's name: ";
	getline(cin, name);
}

class room
{
private:
	vector<student*> students;
	vector<student*>::iterator it;
	vector<student*> MA1;
	vector<student*> MA2;
	vector<student*> MA3;
	vector<student*> MA4;
	vector<student*> MA5;
	vector<student*> MA6;
	vector<student*> MA7;
	vector<student*> MA8;
	int capacity;
	char block;
	
public:
	~room() {
		for (it = students.begin(); it != students.end(); it++)
			delete *it;
	}
	void addstudent(student* s)
	{
		students.push_back(s);
	}
	void addMA1(int target)
	{
		MA1.push_back(students[target]);
		students[target]->setstatus("MA1");
	}
	void delMA1(int target)
	{
		MA1.erase(MA1.begin()+target);
		students[target]->setstatus("UNASSIGNED");
	}
	void delstudent (int target)
	{
		students.erase(students.begin()+target);
		MA1.erase(MA1.begin());
	}
	void printALL()
	{	
		for(int i = 0; i < students.size(); i++)
			students[i]->print(i);
	}
	void printMA1(int target)
	{
		for(int i = 0; i < MA1.size(); i++)
			MA1[i]->print2(i, target);
	}
	int studentsize(){return students.size();}
	int roomMA1size(){return MA1.size();}
};

void mainmenu()
{
	cout << "*===========================================*" << endl <<
	"|[1] add students                           |" << endl <<
	"|[2] delete student                         |" << endl <<
	"|[3] assign rooms                           |" << endl <<
	"|[4] empty rooms                            |" << endl <<
	"|[5] List students                          |" << endl <<
	"|[6] Exit                                   |" << endl <<
	"*===========================================+" << endl << endl << endl;
}

void MAmenu()
{
    cout << "*===========================================*" << endl <<
	"|[1] MA1                                            |" << endl <<
	"|[2] MA2                                            |" << endl <<
	"|[3] MA3                                            |" << endl <<
	"|[4] MA4                                            |" << endl <<
	"|[5] MA5                                            |" << endl <<
	"|[6] MA6                                            |" << endl <<
	"|[7] MA7                                            |" << endl <<
	"|[8] MA8                                            |" << endl <<
	"|[9] back                                           |" << endl <<
	"*===========================================+" << endl << endl << endl;
}

	room r1;
	student* tmp = NULL;
	int target;
	int num;
	bool done = false;
	char choice, option;
	
int main()
{
	do
	{
		mainmenu();
		cout << "Select option (1-6): ";
		cin >> option;
		option = toupper(option);
		switch (option)
		{
			case '1' : 
			cout << "Add student? (Y/N):";
			cin >> choice;
	
			while (toupper(choice) == 'Y')
			{
				tmp = new student;		
				tmp->setstudent();
				r1.addstudent(tmp);		
				cout << "add more students?(Y/N): ";
				cin >> choice;
			}
			r1.printALL();
			break;
			
			case '2' : 
			cout << "Delete student? (Y/N):";
			cin >> choice;
			r1.printALL();
	
			while (toupper(choice) == 'Y')
			{
				cout << "select a student(1-" << r1.studentsize() << "): ";
				cin >> target;
				target--;
							
				r1.delstudent(target);
							
				cout << "Delete more students?(Y/N): ";
				cin >> choice;
				r1.printALL();
			};
			 break;
			 
			case '3' : ;
			cout << "Assign to room? (Y/N):";
			cin >> choice;
			r1.printALL();
			num = 2;
						
			while (toupper(choice) == 'Y' && num > r1.roomMA1size())
			{
				cout << "choose student(1-" << r1.studentsize() << "): ";
				cin >> target;
				target--;
						
				r1.addMA1(target);
						
				cout << "Assign more?(Y/N): ";
				cin >> choice;
				r1.printALL();
				r1.printMA1(target);
			};
				cout << "capacity reached\n\n";
				system("pause");
				break;
				
			case '4' : 
			cout << "Unassign room? (Y/N):";
			cin >> choice;
			r1.printMA1(target);
						
			while (toupper(choice) == 'Y' && num > 0)
			{
				cout << "choose student: ";
				cin >> target;
				target--;
						
				r1.delMA1(target);
						
				cout << "Assign more?(Y/N): ";
				cin >> choice;
				r1.printMA1(target);
					
				num--;
			}
				if (num == 0)
				cout << "room empty\n\n";
				system("pause");	
				break;
			
			case '5' : 
			r1.printALL();
			break;		
		}
	}
	while(!done);

	return 0;
}
just tried the code, it didnt quite work, the id was deleted but then it was duplicated. for example i enter i_d = 13, the results were;

enter id: 13
Lomi15g
Jomi23e
Jomi23e

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
#include <iostream>
#include <string>
#include <vector>
using namespace std; 

class human
{
	private:
			string name ;	
			int id ; 
			char gender ; 
	public:
			human(){};
			human( string n , int i , char g) : name(n) , id (i) , gender(g) { } 
			~human() {}
			void display () ;
			int getid() { return id ; }
			void print(){cout << name << id << gender << endl;}
};
void human::display()
{
		cout<<"\n "<<name<<"\t"<<id<<"\t"<<gender; 
}
int main()
{
	int i_d;
	vector<human> v; 
	v.push_back(human ("Jimmi", 13, 'm') );
	v.push_back(human ("Lomi", 15, 'g') );
	v.push_back(human ("Jomi", 23, 'e') );
	
	cout << "enter id: ";
	cin >> i_d;

	for( vector<human>::iterator i = v.begin() ; i != v.end() ; i++ )
	{
		if( i->getid() == i_d )
		{
			v.erase(i);
			break;
		}
	}
	
	for(int i = 0; i < 3; i++)
	{
		v[i].print();
	}
}
Last edited on
Topic archived. No new replies allowed.