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
|
#include <iostream>
#include <optional>
#include <vector>
#include <map>
#include <algorithm>
// 查找函数
std::optional<int> FindElement(const std::vector<int>& vec, int target)
{
auto it = std::find(vec.begin(), vec.end(), target);
if (it != vec.end())
{
return *it;
}
return std::nullopt;
}
// 安全的除法
std::optional<double> SafeDivide(double a, double b)
{
if (b == 0.0)
{
return std::nullopt;
}
return a / b;
}
// 解析整数
std::optional<int> ParseInt(const std::string& str)
{
try
{
size_t pos;
int value = std::stoi(str, &pos);
// 检查是否整个字符串都被解析
if (pos == str.length())
{
return value;
}
}
catch (const std::exception&)
{
// 解析失败
}
return std::nullopt;
}
// 配置管理
class Config
{
private:
std::map<std::string, std::string> m_Settings;
public:
void Set(const std::string& key, const std::string& value)
{
m_Settings[key] = value;
}
std::optional<std::string> Get(const std::string& key) const
{
auto it = m_Settings.find(key);
if (it != m_Settings.end())
{
return it->second;
}
return std::nullopt;
}
std::optional<int> GetInt(const std::string& key) const
{
auto value = Get(key);
if (value)
{
return ParseInt(*value);
}
return std::nullopt;
}
std::optional<bool> GetBool(const std::string& key) const
{
auto value = Get(key);
if (value)
{
std::string lower = *value;
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
if (lower == "true" || lower == "1" || lower == "yes")
return true;
else if (lower == "false" || lower == "0" || lower == "no")
return false;
}
return std::nullopt;
}
};
void PracticalExamples()
{
std::cout << "=== Practical Examples ===" << std::endl;
// 查找示例
std::vector<int> numbers{1, 3, 5, 7, 9};
auto found = FindElement(numbers, 5);
auto notFound = FindElement(numbers, 6);
std::cout << "Find 5: " << (found ? std::to_string(*found) : "not found") << std::endl;
std::cout << "Find 6: " << (notFound ? std::to_string(*notFound) : "not found") << std::endl;
// 安全除法示例
auto result1 = SafeDivide(10.0, 2.0);
auto result2 = SafeDivide(10.0, 0.0);
std::cout << "10 / 2 = " << (result1 ? std::to_string(*result1) : "undefined") << std::endl;
std::cout << "10 / 0 = " << (result2 ? std::to_string(*result2) : "undefined") << std::endl;
// 解析示例
auto parsed1 = ParseInt("123");
auto parsed2 = ParseInt("abc");
auto parsed3 = ParseInt("123abc");
std::cout << "Parse '123': " << (parsed1 ? std::to_string(*parsed1) : "failed") << std::endl;
std::cout << "Parse 'abc': " << (parsed2 ? std::to_string(*parsed2) : "failed") << std::endl;
std::cout << "Parse '123abc': " << (parsed3 ? std::to_string(*parsed3) : "failed") << std::endl;
// 配置管理示例
Config config;
config.Set("port", "8080");
config.Set("debug", "true");
config.Set("timeout", "30");
config.Set("invalid_number", "abc");
std::cout << "\nConfig examples:" << std::endl;
auto port = config.GetInt("port");
std::cout << "Port: " << (port ? std::to_string(*port) : "not set") << std::endl;
auto debug = config.GetBool("debug");
std::cout << "Debug: " << (debug ? (*debug ? "enabled" : "disabled") : "not set") << std::endl;
auto timeout = config.GetInt("timeout");
std::cout << "Timeout: " << (timeout ? std::to_string(*timeout) : "not set") << std::endl;
auto invalid = config.GetInt("invalid_number");
std::cout << "Invalid number: " << (invalid ? std::to_string(*invalid) : "invalid") << std::endl;
auto missing = config.Get("missing_key");
std::cout << "Missing key: " << (missing ? *missing : "not found") << std::endl;
}
int main()
{
PracticalExamples();
return 0;
}
|