#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#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
static bool lengthFirstCompare(const std::string& a, const std::string& b) {
if (a.size() != b.size()) return a.size() < b.size();
return a < b;
}
int main() {
std::vector<lab::Case> cases;
// Case 1: insert & automatic ascending key order & uniqueness
std::map<std::string, int> employees;
employees["Charlie"] = 35;
employees["Alice"] = 30;
employees["Bob"] = 25;
auto dup = employees.insert({"Bob", 99});
std::string order;
for (const auto& kv : employees) {
if (!order.empty()) order += ",";
order += kv.first + ":" + std::to_string(kv.second);
}
std::string act1 = order + ";dup=" + std::to_string(dup.second ? 1 : 0);
std::string exp1 = "Alice:30,Bob:25,Charlie:35;dup=0";
cases.push_back({
"map_insert_sorted_order",
lab::esc(exp1),
lab::esc(act1),
act1 == exp1
});
// Case 2: find, count, erase, size, clear, empty
std::map<std::string, int> scores;
scores["Alice"] = 90;
scores["Bob"] = 85;
scores.insert({"Charlie", 92});
auto it = scores.find("Bob");
int bob_score = (it != scores.end()) ? it->second : -1;
std::size_t has_bob = scores.count("Bob");
scores.erase("Alice");
std::size_t sz_after_erase = scores.size();
bool empty_before = scores.empty();
scores.clear();
bool empty_after = scores.empty();
std::string act2 = "bob=" + std::to_string(bob_score)
+ ";cnt=" + std::to_string(has_bob)
+ ";sz=" + std::to_string(sz_after_erase)
+ ";eb=" + std::to_string(empty_before ? 1 : 0)
+ ";ea=" + std::to_string(empty_after ? 1 : 0);
std::string exp2 = "bob=85;cnt=1;sz=2;eb=0;ea=1";
cases.push_back({
"map_find_count_erase_clear",
lab::esc(exp2),
lab::esc(act2),
act2 == exp2
});
// Case 3: std::greater descending order & custom std::function comparator
std::map<int, std::string, std::greater<int>> desc_map;
desc_map[1] = "one";
desc_map[3] = "three";
desc_map[2] = "two";
std::string desc_keys;
for (const auto& kv : desc_map) {
if (!desc_keys.empty()) desc_keys += ",";
desc_keys += std::to_string(kv.first);
}
std::map<std::string, int, std::function<bool(const std::string&, const std::string&)>> custom_map(lengthFirstCompare);
custom_map["Charlie"] = 7;
custom_map["Bob"] = 3;
custom_map["Alice"] = 5;
std::string custom_keys;
for (const auto& kv : custom_map) {
if (!custom_keys.empty()) custom_keys += ",";
custom_keys += kv.first;
}
std::string act3 = "desc=" + desc_keys + ";custom=" + custom_keys;
std::string exp3 = "desc=3,2,1;custom=Bob,Alice,Charlie";
cases.push_back({
"map_custom_greater_and_comparator",
lab::esc(exp3),
lab::esc(act3),
act3 == exp3
});
// Case 4: bidirectional forward and reverse iterator traversal
std::string rev_keys;
for (auto rit = employees.rbegin(); rit != employees.rend(); ++rit) {
if (!rev_keys.empty()) rev_keys += ",";
rev_keys += rit->first;
}
std::string exp4 = "Charlie,Bob,Alice";
cases.push_back({
"map_bidirectional_reverse_iter",
lab::esc(exp4),
lab::esc(rev_keys),
rev_keys == exp4
});
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-map-lab/explore-cpp-libs-map.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-map.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2357.zip