-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperson_col.h
More file actions
71 lines (57 loc) · 1.72 KB
/
person_col.h
File metadata and controls
71 lines (57 loc) · 1.72 KB
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
#ifndef PERSON_COL_H
#define PERSON_COL_H
#include <map>
#include <algorithm>
using namespace std;
//Class PersonCol which is a template for storing elements of specified types
//Contains private variable - people
template<typename L>
class PersonCol {
public:
//methods for adding and removing elements from collection
void add_person(L &c) {
unsigned int cardId = c.get_card_id();
if (!has_id(cardId)) {
people.insert(typename map<unsigned int, L>::value_type(cardId, c));
} else {
cout << "Such a person already exists" << endl;
}
}
void delete_person(unsigned int id) {
typename map<unsigned int, L>::iterator it = people.find(id);
if (it != people.end()) {
people.erase(it);
} else {
cout << "Such a person does not exist" << endl;
}
}
//getters
int get_size() const {
return people.size();
}
L &get_person(unsigned int id) {
return people.find(id)->second;
//return people.at(id);
}
const map<unsigned int, L> &get_people() const {
return people;
}
//method for printing informations about all elements
void show_all() {
cout << "People: " << endl;
typename map<unsigned int, L>::iterator it;
for (it = people.begin(); it != people.end(); it++) {
cout << it->second.to_string() << endl;
}
}
//methods for for checking if collection is empty or if person with specified id is in the collection
bool has_id(unsigned int id) const {
return people.find(id) != people.end();
}
bool empty() const {
return people.empty();
}
private:
map<unsigned int, L> people;
};
#endif