-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path11_2_TestMapContainer.cpp
More file actions
98 lines (92 loc) · 2.47 KB
/
11_2_TestMapContainer.cpp
File metadata and controls
98 lines (92 loc) · 2.47 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
#include<map>
#include<string>
#include<iostream>
#include<Windows.h>
using namespace std;
template<typename K, typename V>
void print(const map<K, V>& m1)
{
for (typename map<K, V>::const_iterator iter = m1.cbegin(); iter != m1.cend(); ++iter)
{
//cout << iter->first << "\t" << iter->second << endl;
}
for (const pair<K, V>& kv : m1)
{
cout << kv.first << "\t" << kv.second << endl;
}
}
template<typename K, typename V>
void search_print(const map<K, V>& m1, const K& key)
{
auto iter = m1.find(key);
if (iter != m1.end())
{
cout << key << "--> " << iter->second << "\n";
}
else
{
cout << key << "은(는) 목록에 없습니다.\n";
}
}
void test_map()
{
map<string, double> pitcher_list;
pitcher_list.insert(pair<string, double>("박세웅", 2.23));
pitcher_list.insert(pair<string, double>("해커", 2.93));
pitcher_list.insert(pair<string, double>("피어밴드", 2.95));
pitcher_list.insert(make_pair("차우찬", 3.04));
pitcher_list.insert(make_pair("장원준", 3.05));
pitcher_list.insert(make_pair("헥터", 3.09));
pitcher_list["니퍼트"] = 3.56;
pitcher_list["박종훈"] = 3.76;
pitcher_list["켈리"] = 3.90;
pitcher_list["오승환"] = 3.58;
print(pitcher_list);
search_print(pitcher_list, string("오승환"));
search_print(pitcher_list, string("류현진"));
}
template <typename K, typename V>
void print(const multimap<K, V>& mm)
{
for (const pair<K, V> p : mm)
{
cout << p.first << " " << p.second << endl;
}
}
void test_multimap()
{
multimap<int, string> mm;
mm.insert(make_pair(1, "hello"));
mm.insert(make_pair(1, "hi"));
mm.insert(make_pair(1, "ahihi"));
mm.insert(make_pair(2, "bye"));
//mm.insert(make_pair(2, "baba"));
print(mm);
pair<multimap<int, string>::iterator, multimap<int, string>::iterator> range = mm.equal_range(1);
for (auto equal_iter = range.first; equal_iter != range.second; ++equal_iter)
{
cout << equal_iter->first << " : " << equal_iter->second << endl;
}
auto range2 = mm.equal_range(3);
for (auto equal_iter = range2.first; ; ++equal_iter)
{
// 무효한 반복자이므로 역참조 불가능
//cout << equal_iter->first << " : " << equal_iter->second << endl;
if (equal_iter == range2.second)
{
cout << "empty\n";
break;
}
}
auto range3 = mm.equal_range(2);
for (auto equal_iter = range3.first; equal_iter != range3.second; ++equal_iter)
{
cout << equal_iter->first << " : " << equal_iter->second << endl;
}
}
int main(void)
{
SetConsoleOutputCP(CP_UTF8);
//test_map();
test_multimap();
}