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;
}
相关推荐
硕士复现燃料电池供氢系统建模及控制策略Simulink仿真(附带说明PPT+参考文献+复现模型+实物源码)
内容概要:本资源聚焦于燃料电池供氢系统的建模与控制策略研究,通过Simulink仿真平台实现系统动态行为的精确模拟与控制算法的验证。内容涵盖供氢系统的关键部件建模,如氢气供应管路、压力调节阀、储氢罐及燃料电池堆等,并针对系统响应滞后、压力波动等问题,设计并实现了先进的控制策略,例如基于反馈与前馈的复合控制、PID控制或模型预测控制(MPC),以确保氢气供给的稳定性与效率。配套提供的说明PPT详细阐述了建模思路与控制逻辑,参考文献为研究提供了理论支撑,而完整的复现模型与实物源码则极大地方便了学习者进行仿真复现与二次开发。; 适合人群:具备一定自动化、电气工程或能源动力系统背景,正在进行相关课题研究的研究生,以及从事氢能系统开发的工程技术人员。; 使用场景及目标:① 学习并掌握燃料电池供氢系统的工作原理与关键组件的数学建模方法;② 利用Simulink搭建复杂能源系统的仿真模型,并设计、调试与验证先进控制策略;③ 基于提供的完整资源包(PPT、模型、源码)进行科研复现,作为学位论文或学术研究的技术基础。; 阅读建议:学习者应结合说明PPT的理论讲解,逐步操作复现模型,重点关注控制模块的设计逻辑与参数整定过程。建议在理解现有模型的基础上,尝试修改工况参数或优化控制算法,以深化对系统动态特性的认识,从而将本资源作为开展自主创新研究的有效跳板。
风光火储多源协同参与电网一次调频与二次 AGC 调频控制策略研究(Simulink仿真实现)
内容概要:本文研究了风光火储多源协同参与电网一次调频与二次AGC调频的控制策略,并通过Simulink进行仿真实现。深入分析了风能、光伏、火电及储能系统在电网频率调节中的出力特性与动态响应机制,提出了一种综合协调控制策略,实现调频责任的合理分配与控制指令的有效分解。研究涵盖了多能源系统建模、频率偏差检测、调频功率分配逻辑、控制器参数设计及闭环仿真验证等关键环节,有效提升了高比例新能源接入背景下电力系统的频率稳定性与抗扰动能力。; 适合人群:具备电力系统、自动化或新能源相关背景,从事电网控制、能源调度、仿真建模等工作的科研人员与工程技术人员,尤其适合研究生及从事新能源并网与电力系统稳定研究的专业人员。; 使用场景及目标:①应用于高比例新能源接入电网的频率稳定控制研究;②为风光火储多能互补系统的协同控制策略设计提供仿真验证平台;③支持电力系统仿真教学与科研中的动态性能分析与控制算法开发。; 阅读建议:建议结合Simulink模型文件同步运行仿真,深入理解各模块控制逻辑;重点关注多源调频的协调机制与参数整定方法,可进一步扩展至含储能优化配置或考虑通信延迟的场景研究。
毕业设计含文档和代码VB+ACCESS汽车租凭管理系统(论文+系统)
毕业设计含文档和代码VB+ACCESS汽车租凭管理系统(论文+系统)
poppler-26.09.0
Poppler 是开源 PDF 渲染 / 解析库(基于 Xpdf 3.0),poppler-26.09.0 是它 2026-09-03 发布的正式版本
LangGraph图版本控制:迁移与兼容性处理
内容概要:讲解随着需求变化,LangGraph图结构如何演进。展示状态字段增删改、节点重命名、边调整时的兼容策略。通过版本号字段和迁移函数示例,说明如何处理旧检查点数据。讨论灰度发布和A/B测试中的多图版本并存方案。 sjbzhibo.hqjxgs.com hntxmedia.com www.honglu-cy.com seriea-live-streaming.hnxskj.com cbalive.htne.com.cn


非技术区
