-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_2_TestHashContainer.cpp
More file actions
101 lines (98 loc) · 2.01 KB
/
11_2_TestHashContainer.cpp
File metadata and controls
101 lines (98 loc) · 2.01 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
#include <functional>
#include<unordered_set>
#include<string>
#include<iostream>
using namespace std;
template<typename T>
void print(const unordered_set<T>& us)
{
cout << "-----------" << "\n";
for (const T& elem : us)
{
cout << elem << "\n";
}
cout << "-----------" << "\n";
}
template<typename T>
void has_element(const unordered_set<T>& us, const T val)
{
if (us.find(val) != us.end())
{
cout << val << " 가 존재\n";
return;
}
cout << val << " 가 없다\n";
}
void test_default_uset()
{
unordered_set<string> s;
s.insert("hi");
s.insert("my");
s.insert("name");
s.insert("is");
s.insert("cmj");
s.insert("welcome");
s.insert("to");
s.insert("c++");
print(s);
has_element(s, string("c++"));
has_element(s, string("c"));
string hi = "hi";
cout << "'" << hi << "' 를 삭제\n";
s.erase(s.find(hi));
has_element(s, hi);
hi = "ho";
has_element(s, hi);
// 무효화 된 iterator라서 erase 시 오류 발생
//s.erase(s.find(hi));
}
class Todo
{
int priority;
string desc;
public:
Todo(int _p, string _str) : priority(_p), desc(_str) {}
bool operator==(const Todo& cmp) const
{
if (cmp.desc == desc && cmp.priority == priority)
{
return true;
}
return false;
}
friend ostream& operator<< (ostream& o, const Todo& td);
friend struct hash<Todo>;
};
namespace std
{
template <>
struct hash<Todo>
{
size_t operator()(const Todo& t) const
{
hash<string> hash_func;
return t.priority ^ (hash_func(t.desc));
}
};
}
ostream& operator<< (ostream& o, const Todo& td)
{
return o << "[중요도 : " << td.priority << "] " << td.desc;
}
void test_class_uset()
{
unordered_set<Todo> todos;
todos.insert(Todo(1, "농구 하기"));
todos.insert(Todo(2, "수학 숙제 하기"));
todos.insert(Todo(1, "프로그래밍 프로젝트"));
todos.insert(Todo(3, "친구 만나기"));
todos.insert(Todo(1, "영화 보기"));
print(todos);
has_element(todos, Todo(1, "영화 보기"));
has_element(todos, Todo(2, "영화 보기"));
}
int main(void)
{
// test_default_uset();
test_class_uset();
}