-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSET_MAP.cpp
More file actions
439 lines (362 loc) · 10.3 KB
/
SET_MAP.cpp
File metadata and controls
439 lines (362 loc) · 10.3 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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/*
HOW TO USE STL HASHING, UNORDERED/ORDERED SET/MAP
below are 4 classes with definitions used to explain hashing
and 2 methods for a complex example of a map of a custom
object and set of a custom object.
*/
#include "stdafx.h"
#include <iostream>
#include <string>
#include <unordered_set>
#include <unordered_map>
#include <set>
#include <map>
using namespace std;
// FOR 6.
// Define a custom key object.
// 1. hasher template must be redefined in STD for use in ordered/unordered set/map (SHOWN BELOW)
// 2. operator== must be redefined for use in unordered_set, unordered_map.
// it compares for collisions after getting an index with hashing function.
// 3. operator< is not defined here, but must be defined for use in ORDERED set/map
// it compares objects to sort them. (Definition for it exists in class Human BELOW)
struct Key
{
std::string first;
std::string second;
int third;
Key(string first, string second, int third) {
this->first = first;
this->second = second;
this->third = third;
}
// *required for unordered_set/unordered_map
bool operator==(const Key &other) const
{
return (first == other.first
&& second == other.second
&& third == other.third);
}
};
// FOR 6.
// Redefine a template for a custom hash<Key> object.
// 1. operator() must be redefined to accept object and return hash
namespace std
{
template <>
struct hash<Key>
{
size_t operator()(const Key& k) const
{
// Compute individual hash values for first, second and third
size_t res = 17;
res = res * 31 + hash<string>()(k.first);
res = res * 31 + hash<string>()(k.second);
res = res * 31 + hash<int>()(k.third);
return res;
}
};
}
// FOR 3., 6., 7., 8.
// CUSTOM CLASS HUMAN THAT IS USABLE IN UNORDERED/ORDERED SET/MAP
struct Human
{
string name;
int age;
Human(string Name, int Age) {
name = Name;
age = Age;
}
void print() const {
cout << name << ", " << age << endl;
}
// *required for unordered/ordered set/map
bool operator==(const Human &other) const
{
return (name == other.name && age == other.age);
}
// *required for ordered set/map
bool operator<(const Human &other) const
{
if (age == other.age) {
// if equal age sort by name
return name < other.name;
}
else {
// if age is different sort by age
return (age < other.age);
}
}
};
// FOR 7. and 8.
// CUSTOM HASHER FOR CLASS HUMAN
// *required for unordered/ordered set/map
namespace std
{
template <>
struct hash<Human>
{
size_t operator()(const Human& human) const
{
// Compute individual hash values for first, second and third
size_t res = 17;
res = res * 31 + hash<string>()(human.name);
res = res * 31 + hash<int>()(human.age);
return res;
}
};
}
// FOR 8.
// method accepts a person and his friend and a collection of persons and their friends.
// returns true if 'person' has friend 'fr'
bool hasFriend(const Human& person, const Human& fr, map<Human, set<Human>> &human_friends) {
auto found = human_friends.find(person);
if (found != human_friends.end()) {
set<Human> friends = (*found).second;
for (auto& curFr : friends) {
if (curFr == fr)
return true;
}
}
else {
string errMsg = "Person not found in the collection.";
throw std::invalid_argument(errMsg);
}
return false;
}
// FOR 8.
// prints friendship type of h1 and h2 that are in the collection
void areFriends(const Human& h1, const Human& h2, map<Human, set<Human>>& human_friends)
{
bool h1_to_h2 = hasFriend(h1, h2, human_friends);
bool h2_to_h1 = hasFriend(h2, h1, human_friends);
if (h1_to_h2 && h2_to_h1) {
cout << h1.name <<" and "<< h2.name << " are both friends" << endl;
}
else if (h1_to_h2 && !h2_to_h1) {
cout << h1.name << " is a friend of " << h2.name << endl;
}
else if (h2_to_h1 && !h1_to_h2) {
cout << h2.name << " is a friend of " << h1.name << endl;
}
else {
cout << h1.name << " and " << h2.name << " are not friends" << endl;
}
}
int main()
{
/* 1. SIMPLE INTEGER UNORDERED_SET
unordered_set<int> s;
// insert
for (int i = 0; i < 10; i++) {
int t;
cin >> t;
s.insert(t);
}
// print
for (auto& i : s) {
cout << i << ", ";
}
cout << endl;
// find and print rest to end example
auto found = s.find(3);
if (found != s.end()) {
cout << "found 3, rest are: ";
for (auto it = ++found; it != s.end(); it++)
cout << *it << ", ";
cout << endl;
}
*/
/* 2. UNORDERED_MAP OF SIMPLE KEY, SIMPLE VALUE EXAMPLE
// key, value
unordered_map<string, int> name_age;
// insertion methods
pair<string, int> niki("niki", 14);
name_age.insert(niki);
name_age.insert(make_pair<string, int>("vladi", 14));
name_age.insert({ "pesho", 12 });
name_age.insert({ {"niki", 13},{"pesho", 12},{"gogo", 3} });
name_age["gosho"] = 13;
// niki, 14
// vladi, 14
// pesho, 12
// niki, 13 (does not insert)
// pesho, 12 (does not insert)
// gogo, 3
// gosho, 13
// modify niki to 3
name_age["niki"] = 3;
// print all
for (auto& person : name_age) {
cout << person.first << "," << person.second << endl;
}
*/
/* 3. UNORDERED_MAP OF SIMPLE KEY COMPLEX VALUE EXAMPLE
unordered_map < string, list<Human> > name_friends;
// insert name and lists of Human friends
name_friends.insert({ "vladi", list<Human>() });
list<Human> ivoFriends;
ivoFriends.push_back(Human(40, "Emo"));
ivoFriends.push_back(Human(30, "kremi"));
ivoFriends.push_back(Human(75, "geri"));
name_friends.insert({ "ivo", ivoFriends });
list<Human> kremiFriends;
kremiFriends.push_back(Human(22, "ivo"));
kremiFriends.push_back(Human(30, "kremi"));
kremiFriends.push_back(Human(75, "geri"));
name_friends.insert({ "kremi", kremiFriends });
// print person's name and his friends
for (auto& p : name_friends) {
string currentName = p.first;
list<Human> friends = p.second;
cout << currentName << "'s friends are: " << endl;
for (auto& fr : friends) {
cout << fr.name << ", " << fr.age << endl;
}
cout << endl;
}
*/
/* 4. MAP OF SIMPLE STRING KEY AND SIMPLE STRING VALUE
map<string, string> product_id;
product_id.insert({ "Banana", "hadhwew31" });
product_id.insert({ "Tomato", "hassdwe1" });
product_id.insert({ "Banana", "s" }); // did not
product_id.insert({ "Orange", "a" });
product_id.insert({ "Monkey", "q2" });
product_id.insert({ "Banana", "a" }); // did not
product_id.insert({ "Tomato", "hassdwe1" }); // did not
product_id.insert({ "Tomato", "hassdwe1" }); // did not
// They are without duplicates and sorted by key in alphabetical order
// print
for (auto& column : product_id) {
string key = column.first;
string value = column.second;
cout << key << "'s id is: " << value << endl;
}
cout << endl;
// modifying
cout << "setting 'Banana' value to 'a'" << endl;
product_id["Banana"] = "a";
// print
for (auto& column : product_id) {
string key = column.first;
string value = column.second;
cout << key << "'s id is: " << value << endl;
}
cout << endl;
// searching by key (1) - with .find() - faster
cout << "searching by key 'Banana'" << endl;
auto found = product_id.find("Banana");
if (found != product_id.end()) {
cout << (*found).second << endl;
}
cout << endl;
// searching by key (2) - slower
cout << "searching by key 'Banana'" << endl;
for (auto& column : product_id) {
string key = column.first;
string value = column.second;
if (key == "Banana")
cout << value << endl;
}
cout << endl;
// searching by value - only way
cout << "searching by value 'a'" << endl;
for (auto& column : product_id) {
string key = column.first;
string value = column.second;
if (value == "a")
cout << key << endl;
}
*/
/* 5. MAP OF SIMPLE DOUBLE KEY, SIMPLE STRING VALUE
map<double, string> price_product;
price_product.insert({ 13.50, "Banana" });
price_product.insert({ 21.13, "Dick" });
price_product.insert({ 21.13, "Dickov" });
price_product.insert({ 21.14, "Dick" });
price_product.insert({ 0.30, "Treva" });
price_product.insert({ 1, "Rob" });
// print
for (auto& column : price_product) {
double key = column.first;
string value = column.second;
cout << key << ", "<< value << endl;
}
*/
/* 6. HOW STL HASHING WORKS
std::hash<string> h = std::hash<string>();
cout << "hash of kremi is:"<< h("kremi") << endl;
cout << "hash of kremi is:" << h("kremi") << endl;
cout << "hash of ivo is:" << h("ivo") << endl;
hash<Key> keyhash = hash<Key>();
Key ivan("Ivan", "Mihailov", 22);
cout << "hash of key ivan is: " << keyhash(ivan) <<endl;
cout << "hash of key ivan is: " << keyhash(ivan) << endl;
cout << "The hash of Human kremi is:" << hash<Human>()(Human("Kremi", 22)) << endl << endl;
*/
/* 7. HOW TO CREATE A CUSTOM HASH FOR AN OBJECT AND MAKE A SET OF OBJECT
unordered_set<Human> humans;
humans.insert(Human("Kremi", 22));
humans.insert(Human("Niki", 12));
humans.insert(Human("Emo", 40));
humans.insert(Human("Niki", 24));
for (auto& human : humans) {
human.print();
}
cout << endl;
set<Human> humans2;
humans2.insert(Human("Kremi", 22));
humans2.insert(Human("Niki", 12));
humans2.insert(Human("Emo", 40));
humans2.insert(Human("Niki", 24));
for (auto& human : humans2) {
human.print();
}
*/
/* 8. CREATE MAP OF [CUSTOM OBJECT] AND [SET OF CUSTOM OBJECTS]
Human ivan("Ivan", 22);
Human vladi("Vladi", 22);
Human kremi("Kremi", 20);
Human niki("Niki", 10);
Human nati("Natali", 23);
set<Human> ivanFriends;
set<Human> vladiFriends;
set<Human> kremiFriends;
set<Human> nikiFriends;
set<Human> natiFriends;
ivanFriends.insert(vladi);
vladiFriends.insert(ivan);
vladiFriends.insert(kremi);
kremiFriends.insert(vladi);
natiFriends.insert(ivan);
kremiFriends.insert(nati);
map<Human, set<Human>> human_friends;
human_friends.insert({ ivan, ivanFriends });
human_friends.insert({ vladi, vladiFriends });
human_friends.insert({ kremi, kremiFriends });
human_friends.insert({ niki, nikiFriends });
human_friends.insert({ nati, natiFriends });
// print
for (auto& p : human_friends) {
Human currentPerson = p.first;
set<Human> currentPersonFriends = p.second;
currentPerson.print();
for (auto& fr : currentPersonFriends) {
cout << " +";
fr.print();
}
cout << endl;
}
// print all relations
areFriends(ivan, nati, human_friends);
areFriends(kremi, vladi, human_friends);
areFriends(niki, nati, human_friends);
for (auto& p1 : human_friends) {
for (auto& p2 : human_friends) {
if (!(p1 == p2))
areFriends(p1.first, p2.first, human_friends);
}
}
*/
return 0;
}