看Effective STL Item21时遇到的小问题

kingofark kingofark 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
以下是我看Scott Meyers 《Effective STL》Item21的时候,顺手写的例子程序以实验文章中corrupt container的方法。例子程序中用到了STL泛型算法algorithm中的for_each()函数,但偶然发现用Borland C++ Compiler 5.5编译时,不需要#include<algorithm>就可以编译运行通过;而MSVC6的编译器则很正常,一定需要#include<algorithm>才能编译成功。

哪位大虾知道其中奥妙因果,请不吝赐教。谢谢!

// Test demo according to Effective STL Item 21:
// Always have comparison function return 'false' for equal values.
//
// "... the set concludes that 10A and 10B are NOT equivalent,
// hence NOT the same, and it thus goes about inserting 10B into
// the container alongside 10A. Technically, this action yields
// undefined behavior, but the nearly universal outcome is that
// the set ends up with TWO copies of the value 10, and that means
// it's not a set any longer. By using less_equal as our comparison
// type, we've corrupted the container!"
// ---- Scott Meyers

#include <iostream> // cout
#include <set> // set
#include <functional> // less_equal

// In the case of Borland C++ Compiler 5.5 (BCB5/bcc32.exe),
// the demo still compiles even if we don't add this #include
// for 'for_each', while the help says (the help of course does)
// we must #include<algorithm> for the use of 'for_each'.
// In the case of Visual C++ Compiler (MSVC6/cl.exe), we must
// #include<algorithm> for the use of 'for_each' or we'll get
// error message during compilling.
#include <algorithm> // for_each

using namespace std;

typedef set< int, less_equal<int> > Int_set;

void printout(int& i)
{
cout << i << " ";
}

int main()
{
Int_set s; // s is sorted by "<="

s.insert(10); // insert the value 10

// print out all of 's'
cout << "First insertion: ";
for_each(s.begin(), s.end(), printout);
cout << endl;

s.insert(10); // now try inserting 10 again

cout << "Second insertion: ";
for_each(s.begin(), s.end(), printout);
cout << endl;

return 0;
}
...全文

相关推荐

kof97 for ubuntu

在ubunutu14.04 LTS MAME模拟器下成功运行的kof97.zip。

hang97_

mame下roms中kof97.zip 文件

在ubunutu14.04 LTS + MAME模拟器下成功运行的kof97.zip。

妙龄黄花哥

neogeo.zip

在ubunutu14.04 LTS MAME模拟器下成功运行的neogeo.zip。

hang97_

联邦学习实验复现指南:FedAvg到FedOur三组对比与避坑

联邦学习作为一种分布式机器学习范式,其核心在于多客户端协同训练与模型聚合。通过本地更新与全局聚合的迭代,能在保护数据隐私的同时提升模型泛化能力。在工程实践中,数据划分、客户端采样和聚合策略直接影响实验复现效果。本文基于PyTorch实现,详细解析Cifar-10、MedMNIST和Chest X-Ray三组对比实验,涵盖FedAvg、FedPer、FedRep与FedOur算法,并提供环境搭建、参数配置及常见问题排查指南,助力快速验证算法改动。

weixin_33904756

三相并网逆变器预测电流控制simulink仿真模型

内容概要:本文介绍了一个关于三相并网逆变器预测电流控制的Simulink仿真模型,重点实现了基于预测控制策略的电流环设计,以提升并网电能质量与动态响应性能。该模型涵盖了逆变器主电路、空间矢量脉宽调制(SVPWM)、锁相环(PLL)、坐标变换(如abc/dq0)以及电流预测控制算法等核心模块,能够有效实现对并网电流的精确跟踪与谐波抑制。仿真模型可应用于弱电网条件下的稳定性分析、电流控制策略优化及新型控制算法验证,部分资源附带参考文献与设计文档,便于科研复现与深入研究。; 适合人群:电力电子、电气工程及其自动化等相关专业的高校研究生、科研人员及从事新能源并网技术开发的工程师。; 使用场景及目标:①用于研究三相并网逆变器在不同电网条件下的电流控制性能;②验证模型预测控制(MPC)在电流环中的应用效果;③支撑科研论文的仿真实验部分,辅助完成控制算法的设计与对比分析; 阅读建议:建议结合相关电力电子与电机控制理论基础,按照资源目录顺序逐步学习,重点关注控制算法与仿真模型的对应关系,并利用提供的参考文献深化理解,提升科研效率与创新能力。

欢多天天写代码
评论 7
收藏 已收藏
分享
返回顶部
kingofark
C语言 70,039 成员 243,237 内容