-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_map.cc
204 lines (164 loc) · 4.02 KB
/
test_map.cc
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
/*
* This source code is licensed under the GNU General Public License,
* Version 2. See the file COPYING for more details.
*/
#include <map>
#include <iostream>
namespace Test2 {
struct A {
A();
explicit A(int nr, const std::string &value);
explicit A(const A &other) = delete;
explicit A(A &&other);
~A();
A &operator=(const A &other);
A &operator=(A &&other);
// Or add nr() and str() getters to avoid 'operator<<' being a friend of A
friend std::ostream &operator<<(std::ostream &o, const A &a);
private:
void debug(const char *func, const char *hint, const A *a);
int m_nr;
std::string m_str;
};
A::A(int nr, const std::string &value)
: m_nr{nr}
, m_str{value}
{
debug(__PRETTY_FUNCTION__, "this", this);
}
#if 0
A::A(const A &other)
: m_nr{other.m_nr}
, m_str{other.m_str}
{
debug(__PRETTY_FUNCTION__, this);
}
#endif
A::A(A &&other)
: m_nr{other.m_nr}
, m_str{other.m_str}
{
debug(__PRETTY_FUNCTION__, "this", this);
debug(__PRETTY_FUNCTION__, "other", &other);
other.m_nr = 0;
other.m_str.clear();
}
A::A()
: A(0, "")
{
debug(__PRETTY_FUNCTION__, "this", this);
}
A::~A()
{
debug(__PRETTY_FUNCTION__, "this", this);
}
A &A::operator=(const A &other)
{
if (m_nr != other.m_nr) {
m_nr = other.m_nr;
}
if (m_str != other.m_str) {
m_str = other.m_str;
}
return *this;
}
A &A::operator=(A &&other)
{
std::swap(m_nr, other.m_nr);
std::swap(m_str, other.m_str);
return *this;
}
void A::debug(const char *func, const char *hint, const A *a)
{
std::cout << func << hint << ' ' << a << ' ' << *a << '\n';
}
std::ostream &operator<<(std::ostream &o, const A &a)
{
o << " {nr " << a.m_nr << ", str '" << a.m_str << "'}";
return o;
}
using AMap = std::map<std::string, A>;
#if 0
void test2_1(AMap &amap, const std::string &key, int nr, const std::string &str)
{
A a{nr, str};
amap.insert(std::make_pair(key, a));
}
#endif
#if 0
void test2_2(AMap &amap, const std::string &key, int nr, const std::string &str)
{
amap.insert({key, {nr, str}});
// This produces:
//
// error: converting to ‘const A’ from initializer list would use
// explicit constructor ‘A::A(int, const std::string&)’
//
}
#endif
void test2_3(AMap &amap, const std::string &key, int nr, const std::string &str)
{
amap.insert(std::make_pair(key, A{nr, str}));
}
void test2_4(AMap &amap, const std::string &key, int nr, const std::string &str)
{
A a{nr, str};
std::cout << "ok inserting " << key << a << '\n';
amap.insert(std::make_pair(key, std::move(a)));
}
#if 0
void test2_5(AMap &amap, const std::string &key, int nr, const std::string &str)
{
A a{nr, str};
std::cout << "ok inserting" << key << a;
amap.emplace(std::make_pair(key, std::move(a)));
}
#endif
void dump(AMap &amap)
{
for (const auto &m : amap) {
std::cout << m.first << m.second << '\n';
}
}
} // Test2
void test1()
{
std::map<const char *, int> m1 = {
{ "two", 2 },
{ "one", 1 },
{ "null", 0 },
{ "three", 3 },
{ "seven", 7 },
};
//std::map<std::string,int> m2 = {
// { "one", 1 },
// { "two", 2 }
//};
for (const auto &v : m1) {
//std::cout << v.first << v.second << std::endl;
printf("%s %i\n", v.first, v.second);
}
// Existing keys
printf("value for 'three': %i\n", m1["three"]);
printf("value for 'null': %i\n", m1["null"]);
// Nonexistent key
//printf("value for 'four': %i\n", m1["four"]);
printf("value for 'four': %i\n", m1.find("four") == m1.end());
}
void test2()
{
Test2::AMap amap;
//Test2::test2_1(amap, "key1", 1, "one");
//Test2::test2_2(amap, "key2", 2, "two");
//Test2::test2_3(amap, "key3", 3, "three");
Test2::test2_4(amap, "key4", 4, "four");
//Test2::test2_5(amap, "key5", 5, "five");
//amap.insert(std::make_pair("key6", Test2::A{6, "six"}));
Test2::dump(amap);
}
int main()
{
//test1();
test2();
return 0;
}