#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#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 std::string greet_fn() { return "Hello, World!"; }
struct ScaleFunctor {
int factor;
int operator()(int x) const { return x * factor; }
};
static int add_fn(int a, int b) { return a + b; }
static int sub_fn(int a, int b) { return a - b; }
static bool custom_asc(int a, int b) { return a < b; }
int main() {
std::vector<lab::Case> cases;
// Case 1: std::function wrapping free function, lambda, and functor
std::function<std::string()> f_fn = greet_fn;
std::function<std::string()> f_lam = []() -> std::string { return "Hello, Lambda!"; };
std::function<int(int)> f_obj = ScaleFunctor{3};
std::string act1 = f_fn() + "|" + f_lam() + "|" + std::to_string(f_obj(7));
std::string exp1 = "Hello, World!|Hello, Lambda!|21";
cases.push_back({
"functional_std_function_callables",
lab::esc(exp1),
lab::esc(act1),
act1 == exp1
});
// Case 2: std::bind with placeholders
auto bound_add = std::bind(add_fn, 5, std::placeholders::_1);
auto swapped_sub = std::bind(sub_fn, std::placeholders::_2, std::placeholders::_1);
std::string act2 = "add=" + std::to_string(bound_add(10)) + ";swap_sub=" + std::to_string(swapped_sub(3, 10));
std::string exp2 = "add=15;swap_sub=7";
cases.push_back({
"functional_std_bind_placeholders",
lab::esc(exp2),
lab::esc(act2),
act2 == exp2
});
// Case 3: arithmetic, logical functors and C++17 std::not_fn
int v_plus = std::plus<int>{}(7, 3);
int v_minus = std::minus<int>{}(7, 3);
int v_mul = std::multiplies<int>{}(7, 3);
int v_div = std::divides<int>{}(7, 3);
int v_mod = std::modulus<int>{}(7, 3);
bool l_and = std::logical_and<bool>{}(true, false);
bool l_or = std::logical_or<bool>{}(true, false);
bool l_not = std::logical_not<bool>{}(false);
auto is_even = [](int x) { return x % 2 == 0; };
auto is_odd = std::not_fn(is_even);
bool odd7 = is_odd(7);
std::string act3 = std::to_string(v_plus) + "," + std::to_string(v_minus) + ","
+ std::to_string(v_mul) + "," + std::to_string(v_div) + "," + std::to_string(v_mod)
+ ";and=" + std::to_string(l_and ? 1 : 0)
+ ";or=" + std::to_string(l_or ? 1 : 0)
+ ";not=" + std::to_string(l_not ? 1 : 0)
+ ";not_fn=" + std::to_string(odd7 ? 1 : 0);
std::string exp3 = "10,4,21,2,1;and=0;or=1;not=1;not_fn=1";
cases.push_back({
"functional_arithmetic_and_logical",
lab::esc(exp3),
lab::esc(act3),
act3 == exp3
});
// Case 4: comparison functors with std::sort
std::vector<int> v1 = {5, 3, 9, 1, 4};
std::vector<int> v2 = v1;
std::sort(v1.begin(), v1.end(), custom_asc);
std::sort(v2.begin(), v2.end(), std::less<int>{});
bool asc_eq = (v1 == v2);
std::sort(v2.begin(), v2.end(), std::greater<int>{});
std::string desc_s;
for (std::size_t i = 0; i < v2.size(); ++i) {
if (i) desc_s += ",";
desc_s += std::to_string(v2[i]);
}
bool cmp_ok = std::equal_to<int>{}(4, 4)
&& std::not_equal_to<int>{}(4, 5)
&& std::greater_equal<int>{}(5, 5)
&& std::less_equal<int>{}(3, 5);
std::string act4 = "asc_eq=" + std::to_string(asc_eq ? 1 : 0)
+ ";desc=" + desc_s
+ ";cmp=" + std::to_string(cmp_ok ? 1 : 0);
std::string exp4 = "asc_eq=1;desc=9,5,4,3,1;cmp=1";
cases.push_back({
"functional_comparators_with_sort",
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-functional-lab/explore-cpp-libs-functional.cpp
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-cpp-libs-functional.cpp,或下载右侧课程工程包。 - 确认本地已准备 本地离线 Docker 容器(mstorsjo/llvm-mingw:20260616,G++ 13.3.0 C++17 linux/amd64,--network none)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联99章) · 含当前章节或实例 · mbskycom202612a2307.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2350.zip