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
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <optional>
// 文件操作
std::tuple<bool, std::string> ReadFile(const std::string& filename)
{
std::ifstream file(filename);
if (!file.is_open())
return {false, ""};
std::stringstream buffer;
buffer << file.rdbuf();
return {true, buffer.str()};
}
// 解析CSV行
std::vector<std::string> ParseCSVLine(const std::string& line)
{
std::vector<std::string> fields;
std::stringstream ss(line);
std::string field;
while (std::getline(ss, field, ','))
{
fields.push_back(field);
}
return fields;
}
// 数据库查询模拟
std::tuple<bool, std::map<std::string, std::string>> QueryUser(int userId)
{
// 模拟数据库查询
static std::map<int, std::map<std::string, std::string>> database{
{1, {{"name", "Alice"}, {"email", "alice@example.com"}, {"age", "25"}}},
{2, {{"name", "Bob"}, {"email", "bob@example.com"}, {"age", "30"}}},
{3, {{"name", "Charlie"}, {"email", "charlie@example.com"}, {"age", "35"}}}
};
auto it = database.find(userId);
if (it != database.end())
return {true, it->second};
else
return {false, {}};
}
// 网络请求模拟
std::tuple<int, std::string, std::map<std::string, std::string>> HttpRequest(const std::string& url)
{
// 模拟HTTP请求
if (url.find("api/users") != std::string::npos)
{
return {200, R"({"users": ["Alice", "Bob", "Charlie"]})", {{"Content-Type", "application/json"}}};
}
else if (url.find("api/error") != std::string::npos)
{
return {404, "Not Found", {{"Content-Type", "text/plain"}}};
}
else
{
return {200, "<html><body>Hello World</body></html>", {{"Content-Type", "text/html"}}};
}
}
void PracticalExamplesDemo()
{
std::cout << "=== Practical Examples Demo ===" << std::endl;
// 文件读取
auto [fileSuccess, fileContent] = ReadFile("example.txt");
if (fileSuccess)
{
std::cout << "File content: " << fileContent.substr(0, 50) << "..." << std::endl;
}
else
{
std::cout << "Failed to read file" << std::endl;
}
// CSV解析
std::string csvLine = "John,Doe,30,Engineer,New York";
auto fields = ParseCSVLine(csvLine);
if (fields.size() >= 5)
{
auto [firstName, lastName, age, job, city] = std::make_tuple(
fields[0], fields[1], fields[2], fields[3], fields[4]);
std::cout << "CSV: " << firstName << " " << lastName << ", " << age
<< " years old, " << job << " in " << city << std::endl;
}
// 数据库查询
auto [querySuccess, userData] = QueryUser(1);
if (querySuccess)
{
std::cout << "User data:" << std::endl;
for (const auto& [key, value] : userData)
{
std::cout << " " << key << ": " << value << std::endl;
}
}
// HTTP请求
auto [statusCode, responseBody, headers] = HttpRequest("api/users");
std::cout << "HTTP Response: " << statusCode << std::endl;
std::cout << "Body: " << responseBody.substr(0, 50) << "..." << std::endl;
std::cout << "Headers:" << std::endl;
for (const auto& [headerName, headerValue] : headers)
{
std::cout << " " << headerName << ": " << headerValue << std::endl;
}
}
int main()
{
PracticalExamplesDemo();
return 0;
}
|