質問編集履歴

2

リファレンスmapのリンクと画像を追記

2018/10/27 21:42

投稿

opyon
opyon

スコア1009

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
 
6
6
 
7
+ [リファレンスmap](https://cpprefjp.github.io/reference/map/map.html)
8
+
9
+ ![イメージ説明](33401efe73bc70fa2de09706cea4a4a2.png)
10
+
7
11
  ```C++
8
12
 
9
13
  #include <cstdio>

1

解決

2018/10/27 21:42

投稿

opyon
opyon

スコア1009

test CHANGED
File without changes
test CHANGED
@@ -1,3 +1,117 @@
1
+ ###解決!
2
+
3
+ 回答頂いた@EnumHackさん、@rtr1950xさん、ありがとうございました。理解が深まりました。
4
+
5
+
6
+
7
+ ```C++
8
+
9
+ #include <cstdio>
10
+
11
+ #include <iostream>
12
+
13
+ #include <iterator>
14
+
15
+ #include <string>
16
+
17
+ #include <vector>
18
+
19
+ #include <map>
20
+
21
+
22
+
23
+ template <typename T1, typename T2>
24
+
25
+ bool map_func_key(const std::map<T1, T2> &m, typename std::map<T1, T2>::key_type const &key)
26
+
27
+ {
28
+
29
+ return true; //処理省略
30
+
31
+ }
32
+
33
+
34
+
35
+ template <typename T1, typename T2>
36
+
37
+ bool map_func_value(const std::map<T1, T2> &m, typename std::map<T1, T2>::mapped_type const &value)
38
+
39
+ {
40
+
41
+ return true; //処理省略
42
+
43
+ }
44
+
45
+
46
+
47
+ int main()
48
+
49
+ {
50
+
51
+ std::map<int, int> map1;
52
+
53
+ std::map<std::string, int> map2;
54
+
55
+ std::map<int, std::string> map3;
56
+
57
+ std::map<std::string, std::string> map4;
58
+
59
+ std::map<std::string, std::vector<std::string>> map5;
60
+
61
+
62
+
63
+ map1[1] = 111;
64
+
65
+ map2["b"] = 222;
66
+
67
+ map3[3] = "ccc";
68
+
69
+ map4["4"] = "ddd";
70
+
71
+ map5["5"] = {"555", "eee"};
72
+
73
+
74
+
75
+ // map_func_key
76
+
77
+ std::cout << map_func_key(map1, 1) << std::endl;
78
+
79
+ std::cout << map_func_key(map2, "b") << std::endl;
80
+
81
+ std::cout << map_func_key(map3, 3) << std::endl;
82
+
83
+ std::cout << map_func_key(map4, "4") << std::endl;
84
+
85
+ std::cout << map_func_key(map5, "5") << std::endl;
86
+
87
+
88
+
89
+ // map_func_value
90
+
91
+ std::cout << map_func_value(map1, 111) << std::endl;
92
+
93
+ std::cout << map_func_value(map2, 222) << std::endl;
94
+
95
+ std::cout << map_func_value(map3, "ccc") << std::endl;
96
+
97
+ std::cout << map_func_value(map4, "ddd") << std::endl;
98
+
99
+ std::cout << map_func_value(map5, {"555", "eee"}) << std::endl;
100
+
101
+
102
+
103
+ getchar();
104
+
105
+ return 0;
106
+
107
+ }
108
+
109
+ ```
110
+
111
+
112
+
113
+
114
+
1
115
  ###知りたいこと
2
116
 
3
117
  `template`の使い方でどこが間違っているのか分かりません。