-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmini_xnlo.cpp
172 lines (139 loc) · 4.26 KB
/
mini_xnlo.cpp
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
#include <iostream>
#include <string>
#include <map>
class Knlo {
private:
std::map<std::string, Knlo> m_obj; // 链式调用 ok
std::string m_value; // value 值
public:
// 默认构造函数
Knlo() {}
// 重载[]运算符以实现链式访问
Knlo & operator[](const std::string& key) {
return m_obj[key];
}
// 重载=运算符以设置值
// void operator = (const std::string& val) {
// // (*m_value) = std::string(val);
// m_value = val;
// }
// 重载赋值操作符,用于给 Knlo 对象赋值
Knlo & operator = (const std::string& val) {
m_value = val;
return *this;
}
operator std::string() {
return m_value;
}
// 转换为字符串
/*
std::string toString() const {
std::string result;
for (const auto pair : m_obj) {
result += "\"" + pair.first + "\" > ";
}
// result += (*m_obj)->second.toString();// pair.
result += "\"" + m_value + "\" ; ~~";
return result;
}
*/
// 将 Knlo 对象转换为字符串
std::string str() const {
std::string result = "";
for (auto it = m_obj.begin(); it != m_obj.end();it++) {
if (it != m_obj.begin()) {
// result += ", ";
}
if(!it->second.m_value.empty()){
result += "\"" + it->first + "\" = \"" +it->second.m_value + "\" ; ";
} else {
result += "\"" + it->first + "\" > ";
result += it->second.str();
result += "~ ";
}
}
return result;
}
};
// mini-Pnlo
class Pnlo {
private:
std::map<std::string, Pnlo> m_obj; // 链式调用 ok
std::string m_value; // value 值
public:
// 默认构造函数
Pnlo() {} // ! ok
// 访问操作符的重载,支持链式调用
Pnlo & operator[] (const std::string& key) {
return m_obj[key];
} // ! ok
// 重载赋值操作符,用于给 Pnlo 对象赋值
Pnlo & operator = (const std::string& val) {
m_value = val;
return *this;
}
operator std::string() {
return m_value;
}
// 将 Pnlo 对象转换为字符串
std::string str() const {
std::string result = "";
for (auto it = m_obj.begin(); it != m_obj.end();it++) {
if (it != m_obj.begin()) {
// result += " , ";
}
if(!it->second.m_value.empty()){
result += "\"" + it->first + "\" = \"" +it->second.m_value + "\" ; ";
} else {
result += "\"" + it->first + "\" > ";
result += it->second.str();
result += "~ ";
}
}
return result;
}
};
// mini-Xnlo
typedef std::map<std::string, std::string> xValue;
struct Xnlo : std::map<std::string, Xnlo> {
xValue key;
std::string str() {
std::string str = "";
for (auto it = (this)->key.begin(); it != (this)->key.end(); it++)
{
str += "\"" + it->first + "\" = " + "\"" + key[it->first] + "\" ; ";
}
for (auto it = (this)->begin(); it != (this)->end(); it++)
{
if (it != (this)->begin())
{
// str += "\n";
// std::cout << "\"" << it->first << "\":";
}
if (it != (this)->end())
{
// str += "\n";
// std::cout << "\"" << it->first << "\":";
}
// std::cout << "\"" << it->first << "\" > ";
str += "\"" + it->first + "\" > ";
str += it->second.str();
str += "~";
}
return str;
}
};
int main() {
// API
Xnlo test = Xnlo();
test.key["a"] = "hello";
test["a"]["b"]["c"]["d"].key["ok"] = "ohhhhh!";
test.str(); // 生成xnlo的内容这样
// 这有二个"a" 已经重复键,不推荐用Xnlo
std::cout << test.str() << std::endl;
Pnlo ptest = Pnlo();
ptest["1"] = "hello"; // 不许重复键!
ptest["a"]["b"]["c"]["d"]["ok"] = "ohhhhh!";
ptest.str(); // 生成pnlo的内容这样
std::cout << ptest.str() << std::endl;
}