#include <cstdio>
#include <iostream>
#include <iterator>
#include <list>
#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: Constructors, push/pop at both ends, bidirectional iteration, size/clear/empty
std::list<int> lst = {10, 20, 30};
lst.push_front(5);
lst.push_back(40);
bool ends_ok = (lst.front() == 5 && lst.back() == 40 && lst.size() == 5);
lst.pop_front();
lst.pop_back();
std::vector<int> rev(lst.rbegin(), lst.rend());
bool mid_ok = (lst == std::list<int>{10, 20, 30}) && (rev == std::vector<int>{30, 20, 10});
lst.clear();
bool ok1 = ends_ok && mid_ok && lst.empty();
cases.push_back({
"list_init_ends_and_bidirectional",
lab::jbool(true),
lab::jbool(ok1),
ok1
});
// Case 2: insert, erase, and iterator stability
std::list<int> ls2 = {1, 2, 3, 4, 5};
auto first_it = ls2.begin();
auto it = ls2.begin();
std::advance(it, 2); // points to 3
ls2.insert(it, 10); // {1, 2, 10, 3, 4, 5}, it still valid and points to 3
ls2.erase(it); // {1, 2, 10, 4, 5}, first_it still valid and points to 1
bool ok2 = (*first_it == 1) && (ls2 == std::list<int>{1, 2, 10, 4, 5});
cases.push_back({
"list_insert_erase_and_iterator_stability",
lab::jbool(true),
lab::jbool(ok2),
ok2
});
// Case 3: sort(), unique(), and remove()
std::list<int> ls3 = {3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5};
ls3.sort();
ls3.unique();
bool after_unique = (ls3 == std::list<int>{1, 2, 3, 4, 5, 6, 9});
ls3.remove(5);
bool ok3 = after_unique && (ls3 == std::list<int>{1, 2, 3, 4, 6, 9});
cases.push_back({
"list_sort_unique_and_remove",
lab::jbool(true),
lab::jbool(ok3),
ok3
});
// Case 4: merge() two sorted lists and reverse()
std::list<int> m1 = {1, 3, 5, 7};
std::list<int> m2 = {2, 4, 6, 8};
m1.merge(m2);
m1.reverse();
bool ok4 = m2.empty() && (m1 == std::list<int>{8, 7, 6, 5, 4, 3, 2, 1});
cases.push_back({
"list_merge_and_reverse",
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-list-lab/explore-cpp-libs-list.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-list.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2355.zip