Rob Murray分析的Obfuscated C++, 不懂STL的人可能看不懂。
Rob Murray
. Last Month's Obfuscated C++
Last month we asked you to explain the output of the following program:
#include
#include
using namespace std;
template
void g(int size, op o, const char* sep = "") {
for(int i = 0; i < size; ++i)
cout << o(i) << sep;
}
template
struct w {
char* operator()(int a) {
return g(size,binder1st(op(),a),"\t"),"\n";
}
};
template
void
x(){
g(size, w());
}
int main(){
x,6>();
return 0;
}
This constructor of the x template prints a multiplication table of six rows and six columns, for example:
0 0 0 0 0 0
0 1 2 3 4 5
0 2 4 6 8 10
0 3 6 9 12 15
0 4 8 12 16 20
0 5 10 15 20 25
To understand how this works, we'll first examine the template function g(size,op,sep). op is a function object. The function object is "called" with the first size integers (starting at 0) and the results printed (separated by the string specified by the optional sep argument). We use this function twice.
The first call is in the template function x. It calls g, passing size and an object of type w<op,size>. For our example, this will call g(6,w<multiplies<int>,6>,""), which will cause the following invocations to occur:
w<'multiplies<int>,6>::operator()(0); //rownum = 0
w<multiplies<int>,6>::operator()(1); //rownum = 1
...
w<multiplies<int>,6>::operator()(5); //rownum = 5
Each of these constructors prints a single row of the table. It does this by creating a new function object
binder1st<multiplies<int> >(multiplies(),rownum)
where rownum is the row being printed. This binder1st object is passed to g(), which invokes operator() on the object for each of the integers from 0 to size-1. This prints a single row of the table, using a tab as the separator argument.
binder1st is a standard template class defined in the <functional> header. It takes a binary function object (in this case, multiplies<int>) and a single operand, and creates a new unary function object that "binds" the operand as the first operand of the binary function. This resulting object is invoked by calling operator(), passing a single operand. operator() returns the result of applying the original binary function to the "bound" operand and the operand supplied at the call.
In our example, the row index is "bound" as the first operand. For each column, the invocation of the binder1st object invokes multiplies<int>(int,int), passing the row and column indices respectively; the result is printed by g.
For bonus credit: Explain how the newline at the end of each row is printed.
Parameterizing the operation as a function object allows us to generate tables for other operations. For instance, x<plus<int>,4>() gives us:
0 1 2 3
1 2 3 4
2 3 4 5
3 4 5 6
This Month's Obfuscated C++
This month's Obfuscated C++ tests your knowledge of some of the standard template libraries. What is the output of the following program?
#include <iostream>
#include <list>
#include <iterator>
#include <algorithm>
struct {
void operator()(int i){cout<<i<<"\n";}
} c;
int main(){
list<int> il(2,1);
list<int>::iterator i1(il.begin()),i2(i1);i2++;
back_insert_iterator<list<int> >i3(il);
for(;*i1<20000;i1++,++i2)
*i3=*i1+*i2;
for_each(il.begin(),il.end(),c);
return 0;
}
相关推荐
库存管理基于Excel的多商品组合订单需求拆解模型:SKU级供需预测与四情景压力测试系统设计 Excel 多商品组合订单的需求拆解|Excel模型与模拟数据
内容概要:本文档围绕Excel中多商品组合订单的基础SKU需求拆解,构建从订单行、组合用量表到仓库周需求、期初可供、计划到货及欠交结转的完整计算链条。通过四种组合、六种SKU、两座仓库和八个业务周的模拟数据,演示如何将组合套数准确转换为SKU件数,防止重复扣减与错误状态纳入,并按周递推供需覆盖与欠交变化。文档提供结构化输入校验、公式体系、守恒检查与四情景复算机制,确保结果可追溯、可核对。全部数据为基于固定随机种子生成的原创模拟记录,配套可编辑Excel工作簿与独立Python程序实现双向验证。 适合人群:适用于负责订单汇总、组合商品管理、库存分析、仓配协同、需求计划及业务分析文档编制的相关人员。日常使用者需掌握Excel基本操作如单元格引用、筛选与求和;具备Python基础者可配合标准库运行程序进行批量验证或数据重生成。业务管理岗可重点阅读结果与风险章节,结合工作表索引追踪数值来源。 使用场景及目标:适用于活动套单、同单含组合与单品、多仓备货、到货周期偏移及需求波动等场景。目标是实现组合订单向基础SKU的精准拆解,按周粒度完成供需匹配与欠交递推,识别输入冲突与窗口外到货影响,支持需求增幅、到货延期等多情景模拟分析,所有结论均可从源订单、BOM、Excel公式或独立程序追溯验证。 其他说明:项目交付包含A4文档、可编辑Excel、完整Python代码、CSV输入表、计算快照、四情景结果、测试记录、README指引及作者验收记录。采用固定随机种子确保数据一致性,强调输入有效性校验与双守恒(供给与需求)原则。Excel工作簿采用三层结构(源输入—公式计算—结果展示),支持手动编辑与自动重算,但扩容需重新生成网格。独立Python程序可用于交叉验证,保障模型可靠性。
东汉AD35年世界历史地图矢量文件SHP格式.zip
本资源为《东汉AD35年世界历史地图矢量文件SHP格式》压缩包,文件大小约 29.99 MB,便于集中下载、整理和使用。具体内容请结合压缩包内的实际文件与说明查看。 在当前可读取的目录中,可见JSON 文件、属性表(.dbf)、投影说明文件(.prj)、Shapefile 主文件(.shp)、Shapefile 索引(.shx)。建议解压后保持原有目录结构,先浏览文件清单与说明,再选择需要的资料进行加载或整理。 应用方面,可根据实际资料用于学习练习、资料检索、研究整理或项目参考。使用时可先确定所需内容,建立清晰的文件分类,再按项目要求进行筛选、转换与汇总。 使用 Shapefile 数据时,请把实际配套的 .shp、.shx、.dbf 等文件保存在同一目录,并保持同名关联;加载后再检查坐标系与中文字段显示。如需叠加其它数据,请先统一坐标与字段口径。 数据来源、时间范围、坐标系、字段定义、精度和使用条件,请以实际文件及原始说明为准。本介绍不对文件名之外尚未核实的信息作额外承诺。
AI第三篇:ScaBench-用真实审计竞赛数据构建可复现的 AI 审计评测集
AI第三篇:ScaBench-用真实审计竞赛数据构建可复现的 AI 审计评测集
基于FastAPI Vue3 TypeScript的企业级全栈底座:类型共享、权限完整,三端同源 一套代码交付Web/H5/小程序,AI代码生成 RBAC Docker开箱即用一套开源的快速开发平台
基于FastAPI Vue3 TypeScript的企业级全栈底座:类型共享、权限完整,三端同源。一套完全开源的快速开发平台,提供免费使用。它结合了现代、高性能的技术栈,后端采用Fastapi + SQLAlchemy,前端采用基于 vue3 + typescript + vite + pinia + Element-Plus。
YOLO26算法室内办公环境二维码目标检测+训练好的模型+199张数据集+pyqt可视化界面.zip
附训练好的 YOLO 权重与 PyQt 可视化检测界面,页面底部含可视化效果预览,毕设/课设开箱即用。 【数据集概况】 · 检测类别(中文):[二维码(QRCode)] · 总计:199 张 · 测试集:20 张 · 验证集:40 张 · 训练集:139 张 【训练曲线与评估图】 【模型训练配置】 参数 | 值 模型 | yolo26n 训练轮数 | 100 epochs 输入尺寸 | 640x640 批次大小 | 24 优化器 | auto 初始学习率 | 0.01 训练设备 【关键指标汇总】 训练了 56 个 epoch,最终轮指标: 指标 | 数值 mAP50 | **0.9921** mAP50-95 | 0.8216 Precision | 0.9731 Recall | 1.0000 train/box_loss | 0.6220 train/cls_loss | 0.6579 val/box_loss | 0.7042 val/cls_loss | 0.7743 【训练过程分析】 从整体上看,模型在训练过程中表现稳定,尽管在某些阶段出现了轻微的波折,但总体上遵循了预设的收敛路径。前几轮中,loss 的急剧下降让人感觉仿佛是在玩“过山车”,尤其是在训练的前 10 轮,loss 从初始值 2.0 逐渐降到 1.0 以下,这表明模型在快速学习数据的基本特征。然而,从第 15 轮开始,loss 值并没有进一步明显下降,而是趋于平稳,这表明模型已经初步收敛。 在训练曲线的中期,从第 20 轮到第 35 轮,loss 并没有出现大幅波动或平台期。这个阶段的曲线相对平滑,loss 值基本维持在 0.6 至 0.7 之间,这表明模型的泛化能力在逐渐提升。尽管如此,这个阶段确实出现了一些细微的震荡,特别是在第... 使用场景:毕业设计、课程设计、深度学习入门、工业场景落地验证。


非技术区
