#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <numeric>
#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"; }
std::string join_ints(const std::vector<int>& v) {
std::string out;
for (std::size_t i = 0; i < v.size(); ++i) {
if (i) out += ",";
out += std::to_string(v[i]);
}
return out;
}
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: accumulate (sum & product) and inner_product
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0);
int prod = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>{});
std::vector<int> a = {1, 2, 3};
std::vector<int> b = {4, 5, 6};
int dot = std::inner_product(a.begin(), a.end(), b.begin(), 0);
std::string act1 = "sum=" + std::to_string(sum) + ";prod=" + std::to_string(prod) + ";dot=" + std::to_string(dot);
std::string exp1 = "sum=15;prod=120;dot=32";
cases.push_back({
"numeric_accumulate_and_inner_product",
lab::esc(exp1),
lab::esc(act1),
act1 == exp1
});
// Case 2: partial_sum and adjacent_difference (requires full destination size v.size())
std::vector<int> seq = {1, 2, 3, 4};
std::vector<int> psum(seq.size());
std::partial_sum(seq.begin(), seq.end(), psum.begin());
std::vector<int> adiff(seq.size());
std::adjacent_difference(seq.begin(), seq.end(), adiff.begin());
std::string act2 = "psum=" + lab::join_ints(psum) + ";adiff=" + lab::join_ints(adiff);
std::string exp2 = "psum=1,3,6,10;adiff=1,1,1,1";
cases.push_back({
"numeric_partial_sum_and_adj_diff",
lab::esc(exp2),
lab::esc(act2),
act2 == exp2
});
// Case 3: gcd, lcm, iota, min_element/max_element & average
int g = std::gcd(48, 18);
int l = std::lcm(48, 18);
std::vector<int> iota_v(5);
std::iota(iota_v.begin(), iota_v.end(), 1);
std::vector<int> sample = {3, 1, 4, 1, 5, 9};
int mn = *std::min_element(sample.begin(), sample.end());
int mx = *std::max_element(sample.begin(), sample.end());
int sm = std::accumulate(sample.begin(), sample.end(), 0);
char avg_buf[32];
std::snprintf(avg_buf, sizeof(avg_buf), "%.5f", static_cast<double>(sm) / static_cast<double>(sample.size()));
std::string act3 = "gcd=" + std::to_string(g)
+ ";lcm=" + std::to_string(l)
+ ";iota=" + lab::join_ints(iota_v)
+ ";min=" + std::to_string(mn)
+ ";max=" + std::to_string(mx)
+ ";sum=" + std::to_string(sm)
+ ";avg=" + std::string(avg_buf);
std::string exp3 = "gcd=6;lcm=144;iota=1,2,3,4,5;min=1;max=9;sum=23;avg=3.83333";
cases.push_back({
"numeric_gcd_lcm_iota_minmax",
lab::esc(exp3),
lab::esc(act3),
act3 == exp3
});
// Case 4: C++17 reduce, transform_reduce, inclusive_scan, exclusive_scan
int red = std::reduce(v.begin(), v.end(), 0);
int tred = std::transform_reduce(a.begin(), a.end(), b.begin(), 0);
std::vector<int> inc(seq.size()), exc(seq.size());
std::inclusive_scan(seq.begin(), seq.end(), inc.begin());
std::exclusive_scan(seq.begin(), seq.end(), exc.begin(), 0);
std::string act4 = "reduce=" + std::to_string(red)
+ ";tred=" + std::to_string(tred)
+ ";inc=" + lab::join_ints(inc)
+ ";exc=" + lab::join_ints(exc);
std::string exp4 = "reduce=15;tred=32;inc=1,3,6,10;exc=0,1,3,6";
cases.push_back({
"numeric_cpp17_reduce_and_scans",
lab::esc(exp4),
lab::esc(act4),
act4 == 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-numeric-lab/explore-cpp-libs-numeric.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-numeric.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2362.zip