#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
// Repeat standard headers to demonstrate built-in header-guard idempotency
#include <string>
#include <vector>
namespace lab {
struct Case {
std::string name;
std::string expected_json;
std::string actual_json;
bool passed;
};
std::string esc(const std::string& s) {
std::string out = "\"";
for (unsigned char c : s) {
switch (c) {
case '\"': out += "\\\""; break;
case '\\': out += "\\\\"; break;
case '\b': out += "\\b"; break;
case '\f': out += "\\f"; break;
case '\n': out += "\\n"; break;
case '\r': out += "\\r"; break;
case '\t': out += "\\t"; break;
default:
if (c < 0x20) {
char buf[8];
std::snprintf(buf, sizeof(buf), "\\u%04x", c);
out += buf;
} else {
out += static_cast<char>(c);
}
}
}
out += "\"";
return out;
}
std::string jbool(bool v) { return v ? "true" : "false"; }
void emit(const std::vector<Case>& cases) {
std::cout << "{\"runtime\": \"G++ C++17 (mstorsjo/llvm-mingw:20260616)\", \"cases\": [";
for (std::size_t i = 0; i < cases.size(); ++i) {
if (i) std::cout << ", ";
std::cout << "{\"name\": " << esc(cases[i].name)
<< ", \"expected\": " << cases[i].expected_json
<< ", \"actual\": " << cases[i].actual_json
<< ", \"passed\": " << jbool(cases[i].passed) << "}";
}
std::cout << "]}\n";
}
} // namespace lab
int main() {
std::vector<lab::Case> cases;
// Case 1: <iostream> + <vector>
std::vector<int> vec = {1, 2, 3, 4, 5};
vec.push_back(6);
std::ostringstream oss;
for (std::size_t i = 0; i < vec.size(); ++i) {
if (i) oss << " ";
oss << vec[i];
}
std::string act1 = oss.str();
std::string exp1 = "1 2 3 4 5 6";
cases.push_back({
"include_iostream_and_vector",
lab::esc(exp1),
lab::esc(act1),
act1 == exp1
});
// Case 2: <cmath> + <string>
double root = std::sqrt(4.0);
double c0 = std::cos(0.0);
std::string str = "Hello, C++!";
bool cs_ok = (std::fabs(root - 2.0) < 1e-9) &&
(std::fabs(c0 - 1.0) < 1e-9) &&
(str.length() == 11) &&
(str.substr(7, 3) == "C++");
cases.push_back({
"include_cmath_and_string",
lab::jbool(true),
lab::jbool(cs_ok),
cs_ok
});
// Case 3: <algorithm> std::sort
std::vector<int> unsorted = {5, 3, 1, 4, 2};
std::sort(unsorted.begin(), unsorted.end());
std::ostringstream oss3;
for (std::size_t i = 0; i < unsorted.size(); ++i) {
if (i) oss3 << " ";
oss3 << unsorted[i];
}
std::string act3 = oss3.str();
std::string exp3 = "1 2 3 4 5";
cases.push_back({
"include_algorithm_sort",
lab::esc(exp3),
lab::esc(act3),
act3 == exp3
});
// Case 4: Standard header guard idempotency, std:: namespace, and C++17 macro
int total = std::accumulate(vec.begin(), vec.end(), 0);
bool guard_ok = (total == 21) && (__cplusplus == 201703L);
cases.push_back({
"header_guard_idempotency_and_std_ns",
lab::jbool(true),
lab::jbool(guard_ok),
guard_ok
});
lab::emit(cases);
return 0;
}
本例需在 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。
入口文件:projects/cplusplus-cppo-import-header-file-lab/explore-cppo-import-header-file.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cppo-import-header-file.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2406.zip