看Effective STL Item21时遇到的小问题

kingofark 2002-04-25 11:44:26
以下是我看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;
}
...全文
167 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingofark 2002-04-27
  • 打赏
  • 举报
回复
// Possible reason for BCC55's case:
// In set.h, there's "#include <rw/tree>",
// and in tree.h from 'rw' directory,
// there's "#include <algorithm>",
// thus make 'algorithm' implicitly functions.

我仔细查了一下BCB5目录下的Include目录:
set.h里面有“#include <rw/tree>”;
rw目录的tree.h里面有“#include <algorithm>”;
BCC55似乎就是因此才可以在主程序没有#include <algorithm>的情况下编译通过的。

不知道我说的对不对。请有意者大家明察。谢谢!

再次谢谢大家!
kingofark 2002-04-26
  • 打赏
  • 举报
回复
> 这个问题去看看bcc55的stl实作就可以了,
> 说不定在某个头文件中有#include<algorithm>这条指令

我大略的找过,好像也没找到——当然也可能是我看花了眼。

谢谢大家的关心!
cber 2002-04-26
  • 打赏
  • 举报
回复
这个问题去看看bcc55的stl实作就可以了,说不定在某个头文件中有#include<algorithm>这条指令
chrisXP 2002-04-26
  • 打赏
  • 举报
回复
mark
babysloth 2002-04-26
  • 打赏
  • 举报
回复
其实这跟编译器倒没什么关系,主要是STL实作的问题。BCB5用的RogueWave STL,不够标准。BCB6采用了STLport,就不存在上述的问题了。
garfield_82 2002-04-26
  • 打赏
  • 举报
回复
和tc中不用包含<stdio.h>就能用printf and scanf是不是一样的道理?
garfield_82 2002-04-26
  • 打赏
  • 举报
回复
都是五星级以上的,自卑中.
代码静态分析工具PC-LINT安装配置 PC-Lint是C/C++软件代码静态分析工具,你可以把它看作是一种更加严格的编译器。它不仅可以检查出一般的语法错误,还可以检查出那些虽然符合语法要求但不易发现的潜在错误。 C语言的灵活性带来了代码效率的提升,但相应带来了代码编写的随意性,另外C编译器不进行强制类型检查,也带来了代码编写的隐患。PCLint识别并报告C语言中的编程陷阱和格式缺陷的发生。它进行程序的全局分析,能识别没有被适当检验的数组下标,报告未被初始化的变量,警告使用空指针,冗余的代码,等等。软件除错是软件项目开发成本和延误的主要因素。PClint能够帮你在程序动态测试之前发现编码错误。这样消除错误的成本更低。 使用PC-Lint在代码走读和单元测试之前进行检查,可以提前发现程序隐藏错误,提高代码质量,节省测试间。并提供编码规则检查,规范软件人员的编码行为。 由于PC-LINT对于一般程序员来说可能比较陌生,有好多人安装了也不知道怎样配置和使用。 下面我就根据自己的安装和配置心得对PC-Lint的安装、配置及使用进行下详细说明.本人主要介绍了将PC-Lint集成到VC++6.0和SourceInsight的方法和步骤。 (一)Windows下C/C++开发工具中,VC6使用较为普遍,因此这里先讲下VC6.0环境中集成pclint的步骤. 首先, 当然要下载软件,正版软件要200多$呢,买不起!所以只好网上找免费的拉。从http://www.61ic.com/down/othe/pclint.rar处可以下载到一个8.0版本的pclint. 1.将pclint.rar解压至c:\, 这样lint文件就位与c:\pclint(安装目录)下了。 2.将c:\pclint\lnt 下的3个文件lib-w32.lnt,env-vc6.lnt,co-msc60.lnt拷贝至c:\pclint下, 再在安装目录下创建std.lnt和options.lnt两个文件,其中std.lnt的内容如下 // contents of std.lnt c:\pclint\co-msc60.lnt c:\pclint\lib-w32.lnt c:\pclint\options.lnt -si4 -sp4 -i"D:\Program Files;D:\Program Files\Microsoft Visual Studio\VC98\Include" //end 其中-i后面的路径名为VC的安装路径和VC Include 文件路径,根据自己的修改便可。 options.lnt 内容可为空,为定制内容,以后需要再添加。 准备工作做完了,下一步就是要将pclint集成到VC6中去,先配置lint使之能对单个C或C++文件进行检查。 1.打开VC6,tools--->customize-->tools 新建一个名为pclint的项,在下面填入 command: C:\pclint\lint-nt.exe arguments: -u c:\pclint\std.lnt c:\pclint\env-vc6.lnt "$(FilePath)" Use Output Window 打上勾 close 完成。 这个在你VC窗口tools菜单下应该多了一个pclint选项,可以用它来运行lint程序,对你的c/c++代码进行静态检查了。 现在就可以用个小程序测试一下pclint了 //test1.cpp #include class X { int *p; public: X() { p = new int[20]; } void init() { memset( p, 20, 'a' ); } ~X() { delete p; } }; 编译这个文件,看下你的编译器给你多少警告,再运行下lint, 可以自己对比一下。 我的机器上,VC产生0 errors 0 warnings, 而lint程序产生了如下8条警告信息,有些还是很有用处的提示,这里就不一一分析了. test.cpp(12): error 783: (Info -- Line does not end with new-line) test.cpp(7): error 1732: (Info -- new in constructor for class 'X' which has no assignment operator) test.cpp(7): error 1733: (Info -- new in constructor for class 'X' which has no copy constru

70,040

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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