Rob Murray分析的Obfuscated C++, 不懂STL的人可能看不懂。

myan 2001-02-25 12:43:00
Obfuscated C++

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;
}


...全文
325 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
myan 2001-02-25
  • 打赏
  • 举报
回复
始祖鸟老兄别怪, 我自己能作答的问题从来不给分. 不过,
um! 您的确是只老鸟!
第一个程序原文有问题, 我随后贴出正确程序.
ed9er 2001-02-25
  • 打赏
  • 举报
回复
um!20000这个值设的很好,打出来刚好一屏
ed9er 2001-02-25
  • 打赏
  • 举报
回复
没分?!算了,我来蒙第二个,是不是菲薄那吃数列?除非i++和++i的实现有很大差异,要不应该是的
vcmfc 2001-02-25
  • 打赏
  • 举报
回复
真的看不懂!
vcmfc 2001-02-25
  • 打赏
  • 举报
回复
真的看不懂!
内容概要:本文系统研究了基于豪猪优化算法(CPO)的多无人机协同集群在三维空间中的避障路径规划问题,聚焦于实现以最低成本为目标的航迹优化,综合考虑路径长度、飞行高度、威胁规避及转弯角度等多个关键因素。通过构建精细化的三维环境模型与多无人机协同机制,采用Matlab平台实现CPO算法的仿真与验证,充分展示了该算法在复杂动态障碍环境下的高效搜索能力与全局优化性能。研究不仅涵盖了路径规划的数学建模与目标函数设计,还深入探讨了算法的收敛特性与鲁棒性,为智能群体系统在实际场景中的应用提供了理论依据与技术支撑。; 适合人群:具备一定编程基础和优化算法背景,从事无人机系统控制、智能路径规划、群体协同、人工智能与自动化等相关领域的科研人员、高校研究生及工程技术人员。; 使用场景及目标:①应用于多无人机协同执行侦察、灾害监测、应急救援、区域巡检等复杂任务中的自主路径规划;②为智能优化算法在三维动态环境下的路径决策问题提供可复现的技术范例;③支持研究人员对CPO算法与其他主流群智能算法(如PSO、GWO、WOA等)进行性能对比与改进研究,推动路径规划技术的发展。; 阅读建议:建议结合提供的Matlab代码进行实践操作,重点理解目标函数的多维度建模方式与CPO算法的迭代优化流程,可通过调整环境参数与约束条件进行仿真实验,对比不同算法在相同场景下的路径质量与收敛速度,从而深入掌握其优势与适用边界。
内容概要:本文围绕电动汽车参与电力系统运行备用的能力评估展开深入研究,利用Matlab代码实现对电动汽车集群提供运行备用服务的建模与仿真分析。研究重点在于量化电动汽车作为分布式灵活资源参与电网辅助服务的潜力,通过构建精细化的数学模型,分析其可调功率容量、响应速度、时空分布特性及聚合能力,并采用多面体聚合、内近似模型与闵可夫斯基和等先进方法精确刻画其可调度能力边界。研究进一步结合大规模电动汽车接入场景,探讨其在多时间尺度调度框架下参与调峰、调频等辅助服务的优化策略,评估其对提升高比例可再生能源电网灵活性与稳定性的贡献,最终通过仿真验证所提模型与方法的有效性与实用性。; 适合人群:具备电力系统分析、智能电网、新能源汽车或优化调度等相关专业背景,熟悉Matlab/Simulink仿真工具,从事科研、工程应用的高校研究生、科研人员及电力行业工程师。; 使用场景及目标:①精确评估大规模电动汽车集群在不同约束条件下可提供的运行备用容量;②研究电动汽车在日前、日内及实时调度中的动态响应能力与优化调度策略;③为高渗透率新能源电力系统提供基于移动储能的灵活性资源解决方案,支撑电网安全经济运行。; 阅读建议:建议结合Matlab代码与技术文档同步学习,重点关注多面体聚合建模、能力边界计算及优化调度算法的设计与实现,可进一步拓展至V2G(车辆到电网)、需求响应等互动场景进行二次开发与应用验证。
打开链接下载源码: https://pan.quark.cn/s/a4b39357ea24 OpenCV(开源计算机视觉库)中的DNN(Deep Neural Network)模块是一种功能强大的工具,其目的是用于深度学习模型的操作。该模块使得开发人员能够在OpenCV环境中直接运用已经训练好的深度学习网络,以执行图像识别、目标检测、图像分割等多种功能。DNN模块能够兼容多种深度学习框架的模型,包括TensorFlow、Caffe、ONNX等。 一、DNN模块概述 OpenCV的DNN模块是为了简化深度学习模型的集成过程而专门设计的,它允许开发人员加载预先训练好的神经网络模型,并在图像数据上执行前向传播操作。借助这个模块,用户可以选用GPU或者CPU来提升计算效率,从而构建出高效的应用程序。 二、目标检测案例 在OpenCV的DNN模块中,目标检测是一个常见的应用情形。例如,可以选用SSD(Single Shot Multibox Detector)、YOLO(You Only Look Once)或者 Faster R-CNN 等模型进行实时的目标检测。这些模型能够识别并定位图像中的多个对象,并返回每个对象的类别和边界框坐标。 三、模型转换:PB到PBTXT 在OpenCV中运用TensorFlow模型时,通常需要处理的是`.pb`格式的模型文件,这是TensorFlow的二进制模型文件格式。然而,为了能够读取模型的结构信息,我们需要`.pbtxt`格式的文本文件。转换过程涉及解析`.pb`文件并将其结构信息导出为`.pbtxt`格式,这样做可以让人清晰地了解网络层和参数的配置。在OpenCV中,可以使用`tf.train.write_graph()`函数将.pb...

15,446

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 非技术区
社区管理员
  • 非技术区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧