#include <algorithm>
#include <array>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <stdexcept>
#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: Init, operator[], front/back, data() contiguous storage, size()
std::array<int, 5> arr = {1, 2, 3, 4, 5};
arr[3] = 10;
const int* raw = arr.data();
bool ok1 = (arr.size() == 5) &&
(arr.front() == 1) && (arr.back() == 5) &&
(arr[3] == 10) && (raw[3] == 10) &&
(sizeof(arr) == 5 * sizeof(int));
cases.push_back({
"array_init_access_and_data",
lab::jbool(true),
lab::jbool(ok1),
ok1
});
// Case 2: at() bounds check throws std::out_of_range
std::array<int, 3> small = {1, 2, 3};
int valid_val = small.at(2);
bool threw = false;
try {
(void)small.at(5);
} catch (const std::out_of_range&) {
threw = true;
}
bool ok2 = (valid_val == 3) && threw;
cases.push_back({
"array_at_bounds_check",
lab::jbool(true),
lab::jbool(ok2),
ok2
});
// Case 3: fill() and swap()
std::array<int, 5> filled{};
filled.fill(100);
std::array<int, 3> a1 = {1, 2, 3};
std::array<int, 3> a2 = {4, 5, 6};
a1.swap(a2);
bool ok3 = (filled == std::array<int, 5>{100, 100, 100, 100, 100}) &&
(a1 == std::array<int, 3>{4, 5, 6}) &&
(a2 == std::array<int, 3>{1, 2, 3});
cases.push_back({
"array_fill_and_swap",
lab::jbool(true),
lab::jbool(ok3),
ok3
});
// Case 4: begin()/end() iterators with STL algorithms
std::array<int, 5> unsorted = {50, 10, 40, 20, 30};
std::sort(unsorted.begin(), unsorted.end());
int sum = std::accumulate(unsorted.begin(), unsorted.end(), 0);
bool ok4 = (unsorted == std::array<int, 5>{10, 20, 30, 40, 50}) && (sum == 150);
cases.push_back({
"array_iterators_and_algorithms",
lab::jbool(true),
lab::jbool(ok4),
ok4
});
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-cpp-libs-array-lab/explore-cpp-libs-array.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-array.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2330.zip