forked from learncppnow/9E
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path19.5 SetContactsList.cpp
75 lines (64 loc) · 1.86 KB
/
19.5 SetContactsList.cpp
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
#include <set>
#include <iostream>
#include <string>
using namespace std;
template <typename T>
void DisplayContents(const T& container)
{
for(auto iElement = container.cbegin();
iElement != container.cend();
++ iElement)
cout << *iElement << endl;
cout << endl;
}
struct ContactItem
{
string name;
string phoneNum;
string displayAs;
ContactItem(const string& nameInit, const string & phone)
{
name = nameInit;
phoneNum = phone;
displayAs =(name + ": " + phoneNum);
}
// used by set::find() given contact list item
bool operator ==(const ContactItem& itemToCompare) const
{
return(itemToCompare.name == this->name);
}
// used to sort
bool operator <(const ContactItem& itemToCompare) const
{
return(this->name < itemToCompare.name);
}
// Used in DisplayContents via cout
operator const char*() const
{
return displayAs.c_str();
}
};
int main()
{
set<ContactItem> setContacts;
setContacts.insert(ContactItem("Oprah Winfrey", "+1 7889 879 879"));
setContacts.insert(ContactItem("Bill Gates", "+1 97 7897 8799 8"));
setContacts.insert(ContactItem("Angi Merkel", "+49 23456 5466"));
setContacts.insert(ContactItem("Vlad Putin", "+7 6645 4564 797"));
setContacts.insert(ContactItem("John Travolta", "91 234 4564 789"));
setContacts.insert(ContactItem("Angelina Jolie", "+1 745 641 314"));
DisplayContents(setContacts);
cout << "Enter a name you wish to delete: ";
string inputName;
getline(cin, inputName);
auto contactFound = setContacts.find(ContactItem(inputName, ""));
if(contactFound != setContacts.end())
{
setContacts.erase(contactFound);
cout << "Displaying contents after erasing " << inputName << endl;
DisplayContents(setContacts);
}
else
cout << "Contact not found" << endl;
return 0;
}